알고리즘 풀이/백준

[백준][Java] 1157번 단어 공부

배게 2018. 4. 20. 02:19
728x90

아스키코드를 사용하여 문제를 해결하였습니다.

문제에서 요구하는 출력은 대문자를 출력하므로

숫자로 대문자와 소문자에 맞추어 아스키코드값을 처리해줘야합니다.

alp_num index는 A부터 0에서시작합니다


문자parsing이 끝나고 

최대개수를 가진 알파벳을 찾고,

최대개수가 중복되는 경우를 생각해줍니다.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String input = sc.next();
		int[] alp_num = new int[26];
		int max=0,count=0,most_index=0;
		boolean dup=false;
		
		for(int i=0;i<input.length();i++) {
			int alp_asc = (int)input.charAt(i);
			if(alp_asc>=97 && alp_asc<=122) alp_asc-=97; 
			else if(alp_asc>=65 && alp_asc<=90) alp_asc-=65;
			alp_num[alp_asc]++;
		}
		for(int i=0; i<alp_num.length; i++) if(max<alp_num[i]) max=alp_num[i];
		for(int i=0; i<alp_num.length; i++) {
			if(max==alp_num[i]) {
				count++;
				most_index=i;
			}
		}
		if(count>1) dup=true;
		
		if(dup) System.out.println("?");
		else {
			System.out.println((char)(most_index+65));
		}
	}
}