Jun's Development Journey

[SWEA] 1204. [S/W 문제해결 기본] Array1 - 최빈수 구하기 본문

SWEA/Intermediate

[SWEA] 1204. [S/W 문제해결 기본] Array1 - 최빈수 구하기

J_Jayce 2019. 7. 31. 12:25

문제

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13zo1KAAACFAYh&categoryId=AV13zo1KAAACFAYh&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

풀이

그렇게 어렵지 않은 문제이지만 카테고리별로 기본에 충실해서 차근히 풀어보기 위해 이 문제부터 시작했다.

이 문제는 최대 점수가 100점이므로 101크기의 카운트 배열로 점수 배열의 점수에 맞는 빈도 카운팅을 해준다. 최대 빈도수가 여러 개일 경우 가장 큰 점수값을 출력해야하므로 카운트 배열을 뒤에서부터 탐색하며 값을 구해주면 된다.

 

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
41
42
43
import java.util.*;
public class Solution {
    static Scanner scan = new Scanner(System.in);
    static int[] cnt, scores;
    static int N;
    
 
    static int most_freq_num() {
        int answer = 0;
        int Max=-1;
        cnt = new int[101];//인덱스로 해당 점수 빈도 카운팅
        for(int i=0;i<1000;i++) {
            cnt[scores[i]]++;
            Max = Math.max(Max, cnt[scores[i]]);//최대빈도 갱신
        }
        for(int i=100;i>=0;i--) {
            //최대 빈도 여러개 일 경우 가장 큰 점수 구해야 하므로 뒤에서부터 탐색
            if(Max == cnt[i]) {
                answer = i;
                break;
            }
        }
        return answer;
    }
    static void solution() {
        N = scan.nextInt();
        scores = new int[1000];
        for(int i=1;i<=N;i++) {
            scan.nextInt();
            for(int j=0;j<1000;j++)
                scores[j] = scan.nextInt();
            
            System.out.println("#"+i+" "+most_freq_num());
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        solution();
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter