본문 바로가기
프로그래밍 연구/백준문제

[백준] 1157번 문자열 '단어공부'

by 꽈악 2022. 3. 31.
package backjun;

import java.io.*;

public class Ex1157 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String S;
		
		int[] a = new int[26];
		
		S = br.readLine();
		
		for(int i=0; i<S.length(); i++) {
			int t = S.charAt(i);
			if(t>=65 && t<=90) {
				a[t-65]++;
			} else {
				a[t-97]++;
			}
		}
		
		char result = '?';
		int b = -1;
		
		for(int i=1;i<26;i++) {
			if(b < a[i]) {
				result = (char)(i + 65);
				b=a[i];
			} else if (b == a[i]){
				result = '?';
			}
		}
		
		System.out.println(result);
		br.close();
	}

}

실패...

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next().toUpperCase();

        int[] count = new int[26];

        for (int i = 0; i < str.length(); i++) {
            int num = str.charAt(i) -'A' ;
            count[num]++;
        }

        int max = 0;
        char answer = '?';
        for (int i = 0; i < count.length; i++) {
            if(max < count[i]){
                max = count[i];
                answer = (char)(i+'A');
            } else if (max == count[i]){
                answer = '?';
            }
        }
        System.out.println(answer);
    }
}

 

 

피드백

- sc.next().toUpperCase(); : 대문자 출력하기에 편하다

- 왜 틀렸는지 모르겠다.

'프로그래밍 연구 > 백준문제' 카테고리의 다른 글

[백준] 2908번 상수  (0) 2022.04.12
[백준] 1152번 단어의 개수  (0) 2022.04.11
[백준] 2675번 문자열 반복  (0) 2022.03.29
[백준] 10809번 알파벳 찾기  (0) 2022.03.28