알고리즘 - 백준_10825번_국영수

jodbsgh·2023년 4월 15일
0

입출력 유형 문제풀이가 다 비슷한 것 같다. 이전에 입출력 문제를 몇 번 풀이했던 경험이 있기 때문에 금방 해결할 수 있었다.

✅코드

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

class Student implements Comparable<Student> {
    String name;
    int korean;
    int english;
    int math;

    public Student(String name, int korean, int english, int math) {
        this.name = name;
        this.korean = korean;
        this.english = english;
        this.math = math;
    }

    @Override
    public int compareTo(Student o) {
        if (this.korean != o.korean) {
            return o.korean - this.korean; // 국어 점수 내림차순
        } else if (this.english != o.english) {
            return this.english - o.english; // 영어 점수 오름차순
        } else if (this.math != o.math) {
            return o.math - this.math; // 수학 점수 내림차순
        } else {
            return this.name.compareTo(o.name); // 이름 사전순
        }
    }
}

public class Main {
    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++) {
            String[] input = br.readLine().split(" ");
            String name = input[0];
            int korean = Integer.parseInt(input[1]);
            int english = Integer.parseInt(input[2]);
            int math = Integer.parseInt(input[3]);
            students.add(new Student(name, korean, english, math));
        }

        Collections.sort(students);

        for (Student student : students) {
            bw.write(student.name + "\n");
        }

        bw.flush();
        bw.close();
        br.close();
    }
}
profile
어제 보다는 내일을, 내일 보다는 오늘을 🚀

0개의 댓글