728x90
https://www.acmicpc.net/problem/1157
1157๋ฒ: ๋จ์ด ๊ณต๋ถ
์ํ๋ฒณ ๋์๋ฌธ์๋ก ๋ ๋จ์ด๊ฐ ์ฃผ์ด์ง๋ฉด, ์ด ๋จ์ด์์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋ ์ํ๋ฒณ์ด ๋ฌด์์ธ์ง ์์๋ด๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ๋จ, ๋๋ฌธ์์ ์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ์ง ์๋๋ค.
www.acmicpc.net
โญ ๋ด ์์ค ์ฝ๋
#include<iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
int result[26] = { 0, };
int max = 0, answer;
bool check = true;
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
//์๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ
s[i]=toupper(s[i]);
//ํด๋น ๋ฌธ์์ ๊ฐ์ ์ฆ๊ฐ
result[int(s[i]) - 65]++;
}
//๊ฐ์ฅ ๋ง์ ๋ฌธ์ ์ฐพ๊ธฐ
for (int i = 0; i < 26; i++) {
if (max < result[i]) {
max = result[i];
answer = i;
check=true;
}
//์ต๋ ๊ฐ์๊ฐ ๋์ผํ๊ฒ ๊ฒน์น ๊ฒฝ์ฐ, check๋ฅผ false๋ก ํ๋ฉฐ ? ์ถ๋ ฅ
else if (max == result[i]) {
check = false;
}
}
if (check) {
cout << char(answer+65);
}
else {
cout << '?';
}
return 0;
}
๐ ๋ฌธ์ ํ์ด
- result [0]์ A์ ๊ฐ์, result [1]์ B์ ๊ฐ์... result [25]๋ Z์ ๊ฐ์
- check๋ ์ต๋ ๊ฐ์๊ฐ ๊ฒน์น ๊ฒฝ์ฐ, ?๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ ํ๋๊ทธ
โจ ๋ฌธ์ ํ์ด ๊ฒฐ๊ณผ

728x90