상세 컨텐츠

본문 제목

백준 1157번

백준 알고리즘 풀이

by 발발개발 2020. 12. 16. 09:16

본문

원본 : 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();
	}

}

'백준 알고리즘 풀이' 카테고리의 다른 글

백준 2908번  (0) 2020.12.16
백준 1152번  (0) 2020.12.16
백준 2675번  (0) 2020.12.15
백준 10809번  (0) 2020.12.15
백준 11720번  (0) 2020.12.15

관련글 더보기

댓글 영역