일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 암호론
- 2884
- 자동 형변환
- 현대암호
- 소수판정
- bubble-sort
- lang package
- BufferedWrite
- 객체지향
- try&catch
- 백준
- 백준 알고리즘
- 객체
- 공개키 암호
- 자료구조
- 알고리즘
- 클래스 패스
- JSP
- 재귀호출기본
- 디렉티브
- 형변환 연산자
- OOP
- class
- 예외처리
- HTML
- 프로그래밍
- jvm
- 연결된 예외
- java
- LANG
Archives
- Today
- Total
코드일기장
[백준] 1157번: 단어 공부_Java 본문
제목: 단어 공부
브론즈1
문제
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
입력
첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
출력
첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
예제 입력 1 복사
Mississipi
예제 출력 1 복사
?
예제 입력 2 복사
zZa
예제 출력 2 복사
Z
예제 입력 3 복사
z
예제 출력 3 복사
Z
예제 입력 4 복사
baaa
예제 출력 4 복사
A
🔑 코드
case1:
더보기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int[] alphabet = new int[26];
for (int i = 0; i < str.length(); i++) {
if ('a' <= str.charAt(i) && str.charAt(i) <= 'z') {
alphabet[str.charAt(i) - 'a']++;
} else if ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') {
alphabet[str.charAt(i) - 'A']++;
}
}
int max = 0;
char c = '?';
for (int i = 0; i < alphabet.length; i++) {
if (max < alphabet[i]) {
max = alphabet[i];
c = (char) (i + 'A');
} else if (max == alphabet[i]) {
c = '?';
}
}
System.out.println(c);
}
}
case2:
더보기
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int[] alphabet = new int[26];
method(alphabet, str);
int max = 0;
char c = '\u0000';
for (int i = 0; i < alphabet.length; i++) {
if (max < alphabet[i]) {
max = alphabet[i];
c = (char) (i + 'A');
} else if (max == alphabet[i]) {
c = '?';
}
}
System.out.println(c);
}
static int[] method(int[] a, String str) {
for (int i = 0; i < str.length(); i++) {
if ('a' <= str.charAt(i) && str.charAt(i) <= 'z') {
a[str.charAt(i) - 'a']++;
} else if ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') {
a[str.charAt(i) - 'A']++;
}
}
return a;
}
}
알고리즘 분류
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
대표사진
<a href="https://www.flaticon.com/kr/free-icons/" title="연산 아이콘">연산 아이콘 제작자: Flat Icons - Flaticon</a>
'PS > 백준' 카테고리의 다른 글
[백준] 2581번: 소수_Java (0) | 2022.02.10 |
---|---|
[백준] 5622번: 다이얼_Java (0) | 2022.02.03 |
[백준] 2839번: 설탕 배달_Java (0) | 2022.01.17 |
[백준] 1037번: 약수_Java (0) | 2022.01.13 |
[백준] 1712번: 순익분기점_Java (2) | 2022.01.13 |
Comments