https://www.acmicpc.net/problem/1157

 

가장 많이 사용된 알파벳을 알아내면 되는 문제이다. 

이 문제는 간단하게 map자료형을 사용하여 각 알파벳의 사용횟수를 세고 그 map을 순회하며 만약 최대값이 같은 알파벳이 있다면 ? 를 반환하게 해주면 된다.

 

정답코드

#include "iostream"
#include "string"
#include "map"

using namespace std;

int main()
{
	string s;
	map<char, int> sm;
	cin >> s;

	for (int i = 0; i < s.length(); i++)
	{
		if (islower(s[i])) sm[toupper(s[i])]++;
		else sm[s[i]]++;
	}

	int maxcnt = 0;
	char mosts = '?';

	for (auto& pair : sm)
	{
		if (pair.second > maxcnt) 
		{
			maxcnt = pair.second;
			mosts = pair.first;
		}
		else if (pair.second == maxcnt) {
			mosts = '?';
		}
	}

	cout << mosts << endl;
}

 

+ Recent posts