Updated:

1. 문제 링크

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

2. 사용 알고리즘

정렬

3. 풀이

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

4. 소스 코드

4-1. C++

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<pair<int, int>> v(100003);

bool cmp(const pair<int ,int>& u, const pair<int, int>& v) {
    if (u.second == v.second) return u.first < v.first;
    return u.second < v.second;
}

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 >> v[i].first >> v[i].second;
    sort(v.begin(), v.begin() + n, cmp);
    for (int i = 0; i < n; ++i) cout << v[i].first << " " << v[i].second << "\n";
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/11651.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
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());
        int[][] arr = new int[n][2];
        for(int i = 0; i < n; ++i) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            arr[i][0] = Integer.parseInt(st.nextToken());
            arr[i][1] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(arr, (a1, a2) -> {
            if(a1[1] == a2[1]) return a1[0] - a2[0];
            else return a1[1] - a2[1];
        });
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; ++i) sb.append(arr[i][0]).append(" ").append(arr[i][1]).append("\n");
        System.out.println(sb);
    }
}

Updated:

Leave a comment