Updated:

1. 문제 링크

https://www.acmicpc.net/problem/10825

2. 사용 알고리즘

정렬

3. 풀이

STL의 sort, JAVA의 Arrays.sort를 이용하여 입력받은 수 정렬 후 출력

4. 소스 코드

4-1. C++

https://github.com/dev-aiden/problem-solving/blob/main/boj/10825.cpp

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
#include <iostream>
#include <algorithm>

using namespace std;

struct Score {
    string name;
    int kor;
    int eng;
    int math;
};

Score arr[100003];

bool cmp(const Score& u, const Score& v) {
    if (u.kor == v.kor) {
        if (u.eng == v.eng) {
            if (u.math == v.math) {
                return u.name < v.name;
            }
            return u.math > v.math;
        }
        return u.eng < v.eng;
    }
    return u.kor > v.kor;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> arr[i].name >> arr[i].kor >> arr[i].eng >> arr[i].math;
    sort(arr, arr + n, cmp);
    for (int i = 0; i < n; ++i) cout << arr[i].name << "\n";
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/10825.java

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
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

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());
        Score[] score = new Score[n];
        for(int i = 0; i < n; ++i) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            score[i] = new Score(st.nextToken(), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
        }
        Arrays.sort(score);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; ++i) sb.append(score[i].name).append("\n");
        System.out.println(sb);
    }

    static class Score implements Comparable<Score> {
        private String name;
        private int kor;
        private int eng;
        private int math;

        public Score(String name, int kor, int eng, int math) {
            this.name = name;
            this.kor = kor;
            this.eng = eng;
            this.math = math;
        }

        @Override
        public int compareTo(Score o) {
            if(this.kor == o.kor) {
                if(this.eng == o.eng) {
                    if(this.math == o.math) {
                        return this.name.compareTo(o.name);
                    }
                    return o.math - this.math;
                }
                return this.eng - o.eng;
            }
            return o.kor - this.kor;
        }
    }
}

Updated:

Leave a comment