ArrayList를 사용해서 정렬한뒤 값을 구하면 될줄 알았는데 ArrayList가 아직 숙달 되지 않아서 쓰지 못했다.
arraylist에 값을 저장하는 방법이 있는지, 있으면 어떻게 써야 하는지 찾아봐야겠다.
그냥 배열에 값을 저장하고 문제를 풀었다.
This file contains 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
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args){ | |
int[] cnt = new int[26]; | |
int a =0; | |
int compare = 0; | |
int compare2=0; | |
int repeat=0; | |
Scanner sc = new Scanner(System.in); | |
String input = sc.nextLine(); | |
for(int i=0; i<input.length(); i++) { | |
if(input.charAt(i)<123 && input.charAt(i)>96) { | |
a = input.charAt(i)-97; | |
}else { | |
a= input.charAt(i)-65; | |
} | |
cnt[a] = cnt[a] +1; | |
} | |
for(int i=0; i<cnt.length; i++) { | |
if(compare<=cnt[i]) { | |
compare=cnt[i]; | |
compare2 = i; //제일 많이 쓰인 값이 있는 배열 인덱스 | |
} | |
} | |
for(int i=0; i<cnt.length; i++) { | |
if(compare2!=i) { | |
if(cnt[i]==compare) { | |
repeat=1; | |
} | |
} | |
} | |
if(repeat==1) { | |
System.out.print("?"); | |
}else { | |
System.out.println((char)(compare2+65)); | |
} | |
} | |
} |
'백준 알고리즘 > JAVA' 카테고리의 다른 글
[JAVA 자바] 백준 2292번 벌집 (0) | 2018.07.10 |
---|---|
[JAVA 자바] 백준 2438번 별찍기-1 (0) | 2018.07.10 |
[JAVA 자바] 백준 2675번 문자열 반복 (0) | 2018.07.09 |
[JAVA 자바] 백준 10809번 알파벳 찾기 (0) | 2018.07.09 |
[JAVA 자바] 백준 11654번 아스키 코드 (0) | 2018.07.09 |