C++)
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
bool compare(pair<char, int> m1, pair<char, int> m2) {
if (m1.second == m2.second)
return m1.first > m2.first;
else
return m1.second > m2.second;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string str;
cin >> str;
// 대문자로 변환
transform(str.begin(), str.end(), str.begin(), ::toupper);
vector<pair<char, int>> v;
for (char ch = 'A'; ch <= 'Z'; ch++)
v.push_back({ ch, 0 });
for (auto ch : str)
v.at(ch-'A').second = v.at(ch - 'A').second + 1;
// 알파벳 출현 횟수 기준으로 내림차순 정렬
sort(v.begin(), v.end(), compare);
if (v[0].second == v[1].second)
cout << "?";
else
cout << v[0].first;
return 0;
}
Python)
import sys
str = sys.stdin.readline().rstrip().upper()
if len(str) == 1:
print(str)
else:
dict = {}
for ch in str:
if ch not in dict:
dict[ch] = 1
else:
dict[ch] += 1
arr = sorted(dict.items(), key = lambda item: item[1], reverse = True)
print(arr[0][0] if arr[0][1] > arr[1][1] else '?')'백준 1 > 기초' 카테고리의 다른 글
| [백준 10992] 별 찍기 - 17 (C++) (0) | 2020.12.05 |
|---|---|
| [백준 10991] 별 찍기 - 16 (C++) (0) | 2020.12.05 |
| [백준 2675] 문자열 반복 (C++/Python) (0) | 2020.12.05 |
| [백준 1152] 단어의 개수 (C++/Python) (0) | 2020.12.05 |
| [백준 10809] 알파벳 찾기 (C++/Python) (0) | 2020.12.05 |