원본 : www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
풀이
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String str = reader.readLine().toUpperCase();
int count[] = new int[26];
int index = 0;
for (char ch = 'A'; ch <= 'Z'; ch++) {
int cnt = 0;
if (str.contains(String.valueOf(ch))) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
cnt++;
}
}
}
count[index] = cnt;
index++;
}
index = 0;
int max = count[0];
for (int i = 1; i < count.length; i++) {
if (count[i] > max) {
max = count[i];
index = i;
}
}
int max_count = 0;
for (int i = 0; i < count.length; i++) {
if (max == count[i]) {
max_count++;
}
}
if (max_count == 1) {
writer.write('A' + index);
} else {
writer.write("?");
}
writer.flush();
}
}
댓글 영역