티스토리 뷰

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

class Data {
int id;
int age;
String name;
public Data(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
}

class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(in.readLine());
ArrayList<Data> arrayList = new ArrayList<>();
for(int i=0; i<count; ++i) {
String[] input = in.readLine().split(" ");
int age = Integer.parseInt(input[0]);
String name = input[1];
arrayList.add(new Data(i, age, name));
}
Collections.sort(arrayList, new Comparator<Data>() {
@Override
public int compare(Data first, Data second) {
if(first.age == second.age) {
if(first.id > second.id) {
return 1;
} else {
return -1;
}
} else if(first.age > second.age) {
return 1;
} else {
return -1;
}
}
});
for(int i=0; i<count; ++i) {
System.out.print(arrayList.get(i).age + " ");
System.out.print(arrayList.get(i).name);
System.out.println();
}
}
}


정렬 기준 작성하는게 잘 이해가 안간다... 

처음 생각한 것과 정반대로 했더니 맞았다.



let count = Int(readLine()!)!

var array = [(id: Int, age: Int, name: String)]()

for i in 0..<count {

    let input = readLine()!.split(separator: " ")

    array.append((i, Int(input[0])!, input[1].description))

}

array.sort { (first, second) -> Bool in

    if first.age == second.age {

        return first.id < second.id

    }

    return first.age < second.age

}

for i in array {

    print(i.age, i.name)

}


역시나 Swift론 간단하게 구현 가능했다.

'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글

10989번 '수 정렬하기 3'  (0) 2018.08.05
10825번 '국영수'  (0) 2018.08.05
11651번 '좌표 정렬하기 2'  (0) 2018.08.05
11650번 '좌표 정렬하기'  (0) 2018.08.05
2751번 '수 정렬하기 2'  (0) 2018.08.04
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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
글 보관함