코테/코드트리

[코드트리/NOVICE MID] 총점 비교 (JAVA)

imname1am 2023. 10. 31. 16:04
반응형

🔺 문제

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

 

🔺 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
import java.io.*;
 
class Student implements Comparable<Student> {
    String name;
    int a,b,c;
 
    public Student(String name, int a, int b, int c) {
        this.name = name;
        this.a = a;
        this.b = b;
        this.c = c;
    }
 
    @Override
    public int compareTo(Student st) {    // 총점 기준 오름차순 정렬
        return (this.a + this.b + this.c) - (st.a + st.b + st.c);
    }
}
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        Student[] students = new Student[n];
        for(int i = 0 ; i < n ; i++) {
            String[] str = br.readLine().split(" ");
            students[i] = new Student(str[0], Integer.parseInt(str[1]), Integer.parseInt(str[2]), Integer.parseInt(str[3]));
        }
        
        Arrays.sort(students);
        
        for(Student s : students) {
            System.out.println(s.name + " " + s.a + " " + s.b + " " + s.c);
        }
    }
}
cs

 

 

🧩  해결 아이디어

• Custom Comparator (사용자 정의 정렬)

: 세 과목의 총점 순으로 비교한다. (Line 17)

 

 

 


💬 느낀 점

굿.... 빠르게 정렬해봅시다...

 

1회독 2회독 3회독 4회독 5회독
V        

 

반응형