본문 바로가기
ProgramSolve/Softeer

[Softeer] Lv3. 성적평가 (Java)

by SooooooooS 2024. 2. 4.
728x90

1. 문제


2. 풀이 과정 및 정리

이번 문제의 핵심은 정렬이었던 것 같다.

학생들의 점수를 정렬하여 순위를 매겨야 한다.

단, 점수가 입력된 순서대로 순위를 출력해야하므로 정렬을 해도 원래 위치를 기억해야한다.

 

나는 여기서 Student라는 클래스를 만들어 위치와 점수를 저장하도록 했다.

class Student {
    int idx, score; //학생 인덱스, 점수
    public Student(int idx, int score) {
        this.idx = idx;
        this.score = score;
    }
}

 

이제 Student 타입의 배열을 생성했으므로 이를 정렬해야한다.

나는 이차원 배열을 정렬하기 위해 Comparator 인터페이스를 활용해 왔다.

하지만 이번에는 Comparable 인터페이스도 사용해봤다.

 

이와 같은 2가지 방법으로 내림차순으로 정렬하고 등수는 아래와 같이 구했다.

for(int j = 1; j < n; j++) {
    if(s[j-1].score == s[j].score) { //점수가 같다면 같은 등수
        order[s[j].idx] = order[s[j-1].idx];
    }
    else { //점수가 다르다면 나보다 점수가 높은 사람만큼 등수가 정해진다.
        order[s[j].idx] = j+1;
    }
}
  • 만약 앞에 있는 점수가 같다면 등수는 앞의 등수와 같아야 한다.
  • 앞에 있는 점수와 다르다면 현재보다 점수가 높은 사람들의 수만큼 순위가 매겨진다.

3. 코드

1. Comparator 사용한 코드

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

public class Main {

    static class Student {
        int idx, score; //학생 인덱스, 점수
        public Student(int idx, int score) {
            this.idx = idx;
            this.score = score;
        }
    }

    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());
        StringTokenizer st;

        Student[] total = new Student[n]; //학생의 총 점수
        for(int i = 0; i < n; i++) {
            total[i] = new Student(i, 0);
        }
        
        for(int i = 0; i < 3; i++) {
            st = new StringTokenizer(br.readLine());
            Student[] s = new Student[n];
            for(int j = 0; j < n; j++) {
                s[j] = new Student(j, Integer.parseInt(st.nextToken()));
                total[j].score += s[j].score;
            }

            bw.write(cal(s, n) + "\n");
        }
        
        bw.write(cal(total, n));
        bw.flush();
        bw.close();
    }

    //한 대회에서 학생들의 순위 결정
    static String cal(Student[] s, int n) {
        Arrays.sort(s, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.score - o1.score;
            }
        });
        
        int[] order = new int[n];
        order[s[0].idx] = 1;
        for(int j = 1; j < n; j++) {
            if(s[j-1].score == s[j].score) { //점수가 같다면 같은 등수
                order[s[j].idx] = order[s[j-1].idx];
            }
            else { //점수가 다르다면 나보다 점수가 높은 사람만큼 등수가 정해진다.
                order[s[j].idx] = j+1;
            }
        }

        StringBuilder sb = new StringBuilder();
        for(int j = 0; j < n; j++) {
            sb.append(order[j]+"");
            if(j != n-1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }
}

2. Comparable을 사용한 코드

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

public class Main {

    static class Student implements Comparable<Student> {
        int idx, score;
        public Student(int idx, int score) {
            this.idx = idx;
            this.score = score;
        }

        @Override
        public int compareTo(Student o) {
            return o.score - this.score;
        }
    }

    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());
        StringTokenizer st;

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

            bw.write(cal(s, n) + "\n");
        }
        
        bw.write(cal(total, n));
        bw.flush();
        bw.close();
    }

    static String cal(Student[] s, int n) {
        Arrays.sort(s);
        
        int[] order = new int[n];
        order[s[0].idx] = 1;
        for(int j = 1; j < n; j++) {
            if(s[j-1].score == s[j].score) {
                order[s[j].idx] = order[s[j-1].idx];
            }
            else {
                order[s[j].idx] = j+1;
            }
        }

        StringBuilder sb = new StringBuilder();
        for(int j = 0; j < n; j++) {
            sb.append(order[j]+"");
            if(j != n-1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }
}
728x90