-
1371번. 가장 많은 글자카테고리 없음 2021. 9. 21. 17:22
문제
영어에서는 어떤 글자가 다른 글자보다 많이 쓰인다. 예를 들어, 긴 글에서 약 12.31% 글자는 e이다.
어떤 글이 주어졌을 때, 가장 많이 나온 글자를 출력하는 프로그램을 작성하시오.
입력
첫째 줄부터 글의 문장이 주어진다. 글은 최대 5000글자로 구성되어 있고, 공백, 알파벳 소문자, 엔터로만 이루어져 있다. 그리고 적어도 하나의 알파벳이 있다.
출력
첫째 줄에 가장 많이 나온 문자를 출력한다. 여러 개일 경우에는 알파벳 순으로 앞서는 것부터 모두 공백없이 출력한다.
예제 입력 1
english is a west germanic
language originating in england
and is the first language for
most people in the united
kingdom the united states
canada australia new zealand
ireland and the anglophone
caribbean it is used
extensively as a second
language and as an official l
anguage throughout the world
especially in common wealth
countries and in many
international organizations
예제 출력 1
a
예제 입력 2
baekjoon online judge
예제 출력 2
eno
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] alpha = new int[27]; // 알파벳 카운트 배열! while ( sc.hasNextLine() ) { String str = sc.nextLine(); for(int i = 0; i < str.length(); i++) { int index = str.charAt(i) - 'a'; if( index == -65 ) { continue; } else { alpha[index]++; } } } int max = 0; for (int i = 0; i < 26; i++) { if (max < alpha[i]) { max = alpha[i]; } } for(int i = 0; i < 26; i++) { if(max <= alpha[i]) System.out.print( (char)(i + 'a') ); } sc.close(); } }
자바에서의 EOF 처리가 어떻게 되는지 알아보게 되었다.
EOF를 어떻게 주는지 몰라서 찾아 헤맸던 것 같다.ㅋㅋㅋ;;
while ( sc.hasNextLine() ) { String str = sc.nextLine(); // 불라불라.. }
윈도우 기준 : Ctrl + Z
맥북 기준 : Cmd + D 를 통해서 EOF를 입력할 수 있다고 한다.
반응형