[Softeer] 6250번 [HSAT 5회 정기 코딩 인증평가 기출] 성적 평가 - Java

yseo14·2025년 1월 27일

코딩테스트 대비

목록 보기
49/88



문제링크

풀이

참가자들의 번호와 점수를 저장하기 위해 Participant 클래스를 정의한다.
점수 내림차순으로 Participant 배열을 정렬한다.
순위를 정하기 위해 참가자 수 크기의 int형 배열을 선언한다.
정렬된 배열에서 참가자의 번호를 rank배열에 순서대로 저장한다.
이때, 정렬된 배열에서 이전의 참가자와 점수가 같다면 이전 참가자의 rank를 그대로 저장하고, 다르다면 Participant 배열 인덱스 + 1 값을 저장하면 된다.
최종 등수 또한 위 방법과 마찬가지로 구하면 된다.

코드

코드가 많이 지저분하다. 빠르게 문제를 푸는데에 중점을 두다 보니 이렇게 되었다..

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

public class Main {
    static int n;
    static Participant[] people;
    static Participant[] finalScores;
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(System.out));

        n = Integer.parseInt(br.readLine());
        finalScores = new Participant[n];
        for(int i = 0; i < n; i++){
            finalScores[i] = new Participant(i, 0);
        }
        
        for(int i = 0; i < 3; i++) {
            int[] rank = new int[n];
            people = new Participant[n];
            StringTokenizer st = new StringTokenizer(br.readLine());
            for(int j = 0; j < n; j++){
                int score = Integer.parseInt(st.nextToken());
                finalScores[j].score +=score;
                people[j] = new Participant(j, score);
            }

            Arrays.sort(people, (a, b) -> Integer.compare(b.score, a.score));
                        
            for(int j = 0; j < n; j++){
                int num = people[j].num;
                if(j!=0 && people[j].score == people[j-1].score){
                    rank[num] = rank[people[j-1].num];
                }else{
                    rank[num] = j + 1;
                }                
            }

            for(int j = 0; j < n; j++){
                bw.write(rank[j]+ " ");
            }
            bw.write("\n");
        }

        int[] rank = new int[n];
        Arrays.sort(finalScores, (a, b) -> Integer.compare(b.score, a.score));
        for(int j = 0; j < n; j++){
            int num = finalScores[j].num;
            if(j!=0 && finalScores[j].score == finalScores[j-1].score){
                rank[num] = rank[finalScores[j-1].num];
            }else{
                rank[num] = j + 1;
            }                
        }
        for(int j = 0; j < n; j++){
                bw.write(rank[j]+ " ");
            }
        bw.write("\n");
        
        bw.flush();
        bw.close();
    }

    public static class Participant {
        int num;
        int score;

        public Participant(int num, int score){
            this.num = num;
            this.score = score;
        }
    }
}
profile
like the water flowing

0개의 댓글