1. 캐릭터형 문자는 숫자와 연산이 가능

2. 문장의 마지막만 검사 잘 해주면 됨

import java.util.*;
import java.io.*;
/**
* https://www.acmicpc.net/problem/1371
* BOJ 백준온라인져지 1371 가장 많은 글자 풀이
*/
public class Main {
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main (String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int alphabet[] = new int[26];
String str;
while ((str = br.readLine()) != null) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
alphabet[str.charAt(i) - 'a']++;
}
}
}
int max = 0;
for (int i = 0; i < 26; i++) {
max = Math.max(max, alphabet[i]);
}
for (int i = 0; i < 26; i++) {
if (alphabet[i] == max) {
bw.write('a' + i);
}
}
bw.flush();
}
}
view raw Main.java hosted with ❤ by GitHub

문제

영어에서는 어떤 글자가 다른 글자보다 많이 쓰인다. 에를 들어, 긴 글에서 약 12.31% 글자는 e이다.

어떤 글이 주어졌을 때, 가장 많이 나온 글자를 출력하는 프로그램을 작성하시오.

입력

첫째 줄부터 글의 문장이 주어진다. 글은 최대 5000글자로 구성되어 있고, 공백, 알파벳 소문자, 엔터로만 이루어져 있다. 그리고 적어도 하나의 알파벳이 있다.

출력

첫째 줄에 가장 많이 나온 문자를 출력한다. 여러 개일 경우에는 알파벳 순으로 앞서는 것부터 모두 공백없이 출력한다.


+ Recent posts