풀이방법
사용된 것:
정렬
2022.04.11
간단한 정렬 문제이다.
Comparator Overriding으로 풀면 된다.
코드
Java(2022.04.11)
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{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Member[] list = new Member[n];
for(int i = 0; i<n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
list[i] = new Member(i, Integer.parseInt(st.nextToken()), st.nextToken());
}
Arrays.sort(list, new Comparator<Member>(){
@Override
public int compare(Member o1, Member o2) {
// TODO Auto-generated method stub
int age1 = o1.age;
int age2 = o2.age;
int num1 = o1.num;
int num2 = o2.num;
if(age1>age2) return 1;
else if(age1 == age2){
if(num1>num2) return 1;
if(num1 == num2) return 0;
else return -1;
}
else return -1;
}
});
StringBuilder sb = new StringBuilder();
for(Member m : list) {
sb.append(m.age + " " + m.name + System.lineSeparator());
}
System.out.print(sb);
}
}
class Member {
int num;
int age;
String name;
public Member(int num, int age, String name) {
this.num = num;
this.age = age;
this.name = name;
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
#10828 : 스택 (0) | 2022.04.12 |
---|---|
#1873 : 스택 수열 (0) | 2022.04.12 |
#1920 : 수 찾기 (0) | 2022.04.10 |
#10250 : ACM 호텔 (0) | 2022.04.06 |
#2631 : 줄세우기 (0) | 2022.04.05 |