알고리즘/SWEA

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

Ellie67 2022. 11. 7. 14:05

https://swexpertacademy.com/main/main.do

 

SW Expert Academy

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

swexpertacademy.com

 

SWEA 1204번

1000명의 학생의 점수를 입력 받아 점수 중 최빈수를 구하는 문제

점수는 0~100점이기 때문에 101 사이즈의 배열을 만들어서 해당 점수 값을 증가시킴

import java.util.Scanner;

public class P1204 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int t=1; t<=T; t++){
            int n = sc.nextInt();
            int[] a = new int[101];
            int max = 0, ans = 0;
            for(int i=0; i<1000; i++){
                int k = sc.nextInt();
                a[k]++;
            }
            for(int i=0; i<a.length; i++){
                if(max <= a[i]) { // 최빈수가 여러 개일 경우 가장 큰 점수
                    max = a[i];
                    ans = i;
                }
            }
            System.out.println("#"+n+" "+ans);
        }
    }
}