알고리즘/SWExpert알고리즘

[SWEA] D2 - 1204번 최빈수 구하기 문제

Fenderblue 2023. 4. 29. 22:51

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13zo1KAAACFAYh 

 

SW Expert Academy

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

swexpertacademy.com

 

설명은 생략..

 

 

package D2;

import java.io.*;
import java.util.StringTokenizer;

public class SW_1204 {  //최빈수 구하기 문제
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;
        int T = Integer.parseInt(br.readLine());
        int[] scores;    //0~100사이의 점수가 1000개 주어짐
        for (int i = 1; i <= T; i++) {
            scores = new int[101];
            int t = Integer.parseInt(br.readLine());    //걍 테스트케이스 번호밍
            st = new StringTokenizer(br.readLine());

            for (int j = 0; j < 1000; j++) {
                scores[Integer.parseInt(st.nextToken())] += 1;
            }

            int maxNum = 0; //최빈수
            int maxNumIndex = 0;
            for (int j = 0; j <= 100; j++) {
                if (scores[j] >= maxNum) {  //>=로해줘야 같은 최빈수일경우 더 큰 점수가 나옴
                    maxNum = scores[j]; //최빈수 갱신
                    maxNumIndex = j;    //최빈수의 index(점수값) 갱신
                }
            }
            bw.write("#" + t + " " + maxNumIndex);
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }
}