

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [JAVA] baekjoon : 1157" 단어공부 | |
package javaProfessioner; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class Main1157{ | |
public static void main(String args[]) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); // 문자열을 입력받고 | |
str = str.toUpperCase(); // 전체를 다 대문자로 변환해준다. ( 대소문자의 구분이 없어야 하므로 ) | |
int[] arr = new int[26]; // 알파벳은 총 26개 이므로 26개의 배열 선언 ( 각 알파벳 개수를 세기 위한 배열 ) | |
int max=0; // 가장 많이 나온 알파벳의 개수 | |
int index=0; // 가장 많이 나온 알파벳 인덱스 | |
for(int i = 0 ; i < str.length(); i ++) | |
{ | |
int alpha = str.charAt(i); // alpha변수에 한문자씩 넣기 | |
arr[alpha-65]++; // 대문자 A의 ASCII code 값은 65임. 입력받은 문자가 a,A(toUpperCase 함수로 인해 대문자로 변환)라고 치면 | |
} // 65 - 65 = 0 이므로 위의 arr 배열의 0번째에는 a부터 순차적으로 개수를 샌다. A ~ Z 까지( 이해 꼭 해야함 ) | |
for(int i = 0 ; i < arr.length; i ++) // 배열의 길이만큼 반복 | |
{ | |
if(max < arr[i]) | |
{ | |
max = arr[i]; // 가장 많은 알파벳을 max로 지정 | |
index = i+65; // 가장 많은 알파벳의 수가 있는 배열의 index번호 + 65 를 하면 그 알파벳의 ASCII code가 나옴 | |
} | |
else if(max == arr[i]) // 만약 가장 많은 알파벳의 개수의 합이 같다면 idx는 ? 로 지정 | |
index = '?'; | |
} | |
System.out.println((char)index); // idx 출력 | |
} | |
} |
'알고리즘 > JAVA' 카테고리의 다른 글
[JAVA] 백준 : 10798" 세로읽기 (0) | 2019.08.15 |
---|---|
[JAVA] 백준 : 5585" 거스름돈 (0) | 2019.07.31 |
[JAVA] 백준 : 10773" 제로 (0) | 2019.07.31 |
[JAVA] 백준 : 2439" 별찍기-2 (0) | 2019.07.30 |
[JAVA] 백준 : 10818" 최소, 최대 (0) | 2019.07.30 |