-
11652) 카드 - JAVA백준코딩일기 2020. 3. 8. 16:51
문제 ) https://www.acmicpc.net/problem/11652
풀이 )
카드의 개수와 카드에 적혀있는 정수의 범위를 잘 봐야할 것같다.
Integer Long코드 )
123456789101112131415161718192021222324252627282930313233343536373839404142import java.io.*;import java.util.*;public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));//준규가 갖고있는 숫자카드 개수int N = Integer.parseInt(br.readLine());//숫자카드에 적힌 정수를 모두 card 배열에 저장long[] card = new long[N];for(int i=0; i<N; i++) {card[i] = Long.parseLong(br.readLine());}// 먼저 숫자카드를 오름차순으로 정렬Arrays.sort(card);// cnt = 같은 수의 숫자카드 개수를 셈 (적어도 1장 이상이므로 1로 초기화)// Max = 같은 수가 가장 많은 숫자카드의 개수를 저장. => 적어도 1장 이상이므로 Max값도 1로 초기화int cnt=1, Max=1;long ans = card[0]; // 같은 수가 가장 많은 숫자카드의 정수를 저장// 카드의 개수가 1인경우에는 card[0]이 답이 되므로 ans = card[0] 으로 초기화for(int i=1; i<N; i++) {// 현재 카드가 이전의 카드 정수와 같다면if( card[i] == card[i-1])cnt += 1; // 개수를 하나 올림else //현재카드와 이전의 카드가 다르면cnt = 1; //개수는 1로 유지// 만약에 같은 카드의 개수(cnt)가 이전까지의 Max값보다 크다면if( Max < cnt ) {Max = cnt; // Max 값에 cnt를 저장하고ans = card[i]; // ans=card[i] 를}}bw.write(ans+"\n");br.close(); bw.flush(); bw.close();}} cs'백준코딩일기' 카테고리의 다른 글
10807) 개수 세기 c++ (0) 2020.10.17 10808) 알파벳 개수 C++ (0) 2020.10.16 10825) 국영수 - Java (0) 2020.02.19 17103) 골드바흐 파티션 - Java (0) 2020.01.29 9020) 골드바흐의 추측 (0) 2020.01.29