백준 10825 국영수

·2023년 1월 17일
0

문제

도현이네 반 학생 N명의 이름과 국어, 영어, 수학 점수가 주어진다. 이때, 다음과 같은 조건으로 학생의 성적을 정렬하는 프로그램을 작성하시오.

1. 국어 점수가 감소하는 순서로
2. 국어 점수가 같으면 영어 점수가 증가하는 순서로
3. 국어 점수와 영어 점수가 같으면 수학 점수가 감소하는 순서로
4. 모든 점수가 같으면 이름이 사전 순으로 증가하는 순서로 (단, 아스키 코드에서 대문자는 소문자보다 작으므로 사전순으로 앞에 온다.)


코드

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

public class Main {
    public static class Student implements Comparable<Student>{
        String name;
        int Korean;
        int English;
        int Math;

        public Student(String input){
            StringTokenizer st = new StringTokenizer(input);
            this.name=st.nextToken();
            this.Korean = Integer.parseInt(st.nextToken());
            this.English = Integer.parseInt(st.nextToken());
            this.Math = Integer.parseInt(st.nextToken());
        }

        @Override
        public int compareTo(Student another){
            if(this.Korean!=another.Korean)
                return another.Korean-this.Korean;
            if(this.English!=another.English)
                return this.English- another.English;
            if(this.Math!=another.Math)
                return another.Math - this.Math;

            return this.name.compareTo(another.name);
        }
    }

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

        List<Student> students = new ArrayList<>();
        for (int i = 0; i < n; i++)
            students.add(new Student(br.readLine()));

        students.stream()
                .sorted()
                .map(o->o.name)
                .forEach(System.out::println);
    }
}

해결 과정

  1. Student 클래스를 만들고 Comparable<> 인터페이스를 구현해줬다.

  2. 😁

profile
渽晛

0개의 댓글