728x90
문제
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
구현
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
|
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String word = sc.next().toUpperCase();
int[] alphabet = new int[26];
for (int i = 0; i < word.length(); i++) {
alphabet[word.charAt(i) - 'A']++;
}
int max = 0;
int idx = 0;
for (int i = 0; i < alphabet.length; i++) {
if (alphabet[i] > max) {
max = alphabet[i];
idx = i;
}
}
char res = (char) (idx + 'A');
for (int i = 0; i < alphabet.length; i++) {
if (max == alphabet[i] && idx != i) {
res = '?';
}
}
System.out.println(res);
}
}
|
cs |
결과
728x90
'Algorithm > Beakjoon' 카테고리의 다른 글
[Java] baekjoon 10871 : X보다 작은 수 / for문 (0) | 2021.07.29 |
---|---|
[Java] baekjoon 14681 : 사분면 고르기 / if문 (0) | 2021.07.28 |
[Java] baekjoon 2884 : 알람 시계 / if문 (0) | 2021.07.26 |
[Java] baekjoon 15596 : 정수 N개의 합 / 함수 (0) | 2021.07.25 |
[Java] baekjoon 2439 : 별 찍기 - 2 / for문 (0) | 2021.07.24 |
댓글