Algorithm/이코테

23.국영수

마닐라 2022. 1. 10. 10:14

📍 문제 설명

💡 접근

조건에 따라 정렬시키면 된다.

 

👩‍💻 코드

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);

        int n = kb.nextInt();
        ArrayList<Student> list = new ArrayList<Student>();
        for(int i = 0; i < n; i++) {
            list.add(new Student(kb.next(), kb.nextInt(), kb.nextInt(), kb.nextInt()));
        }
        Collections.sort(list);

        for(Student s : list) {
            System.out.println(s.name);
        }

    }

    private static class Student implements Comparable<Student> {
        private String name;
        private int korean;
        private int english;
        private 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) {
                if(this.english == o.english) {
                    if(this.math == o.math) {
                        return this.name.compareTo(o.name);
                    }
                    return o.math - this.math;
                }
                return this.english - o.english;
            }
            return o.korean - this.korean;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", korean=" + korean +
                    ", math=" + math +
                    ", english=" + english +
                    '}';
        }
    }
}

'Algorithm > 이코테' 카테고리의 다른 글

25.실패율  (0) 2022.01.10
24.안테나  (0) 2022.01.10
22.블록 이동하기  (0) 2022.01.09
21.인구이동  (0) 2022.01.09
20.감시 피하기  (0) 2022.01.09