Recent Posts
Notice
No Rules Rules
단어 공부 (feat. 백준, 1157번) 본문
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;
}
// *&)*@*
- 계산의 편리성을 위해 소문자는 대문자로 치환하였습니다.
- 문자열 중 가장 많은 문자의 개수(max_ans)를 기록합니다.
- 가장 많은 문자는 무엇이고(ans) 몇개(count)가 있는지 확인합니다.
- 위 개수(count)가 1보다 크다면 '?' 를, 아니라면 가장 많은 문자(ans)를 출력합니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
상수 (feat. 백준, 2908번) (0) | 2022.08.11 |
---|---|
단어의 개수 (feat. 백준, 1152번) (0) | 2022.08.11 |
문자열 반복 (feat. 백준, 2675번) (0) | 2022.08.11 |
알파벳 찾기 (feat. 백준, 10809번) (0) | 2022.08.11 |
숫자의 합 (feat. 백준, 11720번) (0) | 2022.08.11 |
Comments