언어, 알고리즘 공부/백준
[백준 알고리즘] 1157번 단어공부 (Java)
쿠몬e
2019. 9. 5. 21:33
<코드>
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
33
34
35
36
37
38
39
40
|
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.nextLine();
int[] alpha = new int[26];//알파벳
for(int i=0; i<word.length();i++) {
char a = (char)word.charAt(i);
if(a>='a') //소문자
alpha[a-'a']++;
else
alpha[a-'A']++;
}
int max = 0;
int ch=0;//알파벳 배열에서 최댓값 가지는 위치
int count=0;
for(int i=0; i<alpha.length;i++) {
if(alpha[i]>max) {
max = alpha[i]; //알파벳 중 가장 많이 나온 문자
ch=i;
}
}
for(int i=0;i<alpha.length;i++) {
if(max==alpha[i]&&ch!=i)
count++;
}
if(count!=0)
System.out.println('?');
else
System.out.println((char)('A'+ch));
}
}
|
링크
반응형