No Rules Rules

단어 공부 (feat. 백준, 1157번) 본문

생활/코테

단어 공부 (feat. 백준, 1157번)

개발하는 완두콩 2022. 8. 11. 12:46
728x90
반응형

단어 공부
https://www.acmicpc.net/problem/1157

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <cstring>
using namespace std;
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	string str;
	char tmp, ans;
	register int arr[26]{ 0 }, max_ans = 0, count = 0;
	cin >> str;
	for (register int i = 0; i < str.size(); ++i) {
		tmp = str.at(i);
		if ('a' <= tmp && tmp <= 'z')
			tmp = 'A' + (tmp - 'a');
		++arr[tmp - 'A'];
		max_ans = max(max_ans, arr[tmp - 'A']);
	}
	for (register int i = 0; i < 26; ++i)
		if (arr[i] == max_ans)
			ans = i + 'A', ++count;
	if (count > 1)
		cout << "?";
	else
		cout << ans;
	return 0;
}
// *&)*@*
  1. 계산의 편리성을 위해 소문자는 대문자로 치환하였습니다.
  2. 문자열 중 가장 많은 문자의 개수(max_ans)를 기록합니다.
  3. 가장 많은 문자는 무엇이고(ans) 몇개(count)가 있는지 확인합니다.
  4. 위 개수(count)가 1보다 크다면 '?' 를, 아니라면 가장 많은 문자(ans)를 출력합니다.
728x90
반응형
Comments