728x90
문제
https://www.acmicpc.net/problem/10814
풀이 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static class Member implements Comparable<Member>{
int num;
int age;
String name;
public Member(int num, int age, String name) {
this.num = num;
this.age = age;
this.name = name;
}
@Override
public int compareTo(Member o) {
if (this.age == o.age) {
return this.num - o.num;
} else {
return this.age - o.age;
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Member[] members = new Member[n];
StringBuilder sb = new StringBuilder();
int num = 0; //가입 순서 저장용
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int age = Integer.parseInt(st.nextToken());
String name = st.nextToken();
members[i] = new Member(num++, age, name);
}
Arrays.sort(members);
for (Member member : members) {
sb.append(member.age).append(" ").append(member.name).append("\n");
}
System.out.println(sb);
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[알고리즘/백준] 10828 스택(자바) (0) | 2021.10.01 |
---|---|
[알고리즘/백준] 10816 숫자 카드 2(자바, Map) (0) | 2021.09.30 |
[알고리즘/백준] 10773 제로(자바, 스택) (0) | 2021.09.30 |
[알고리즘/백준] 10250 ACM 호텔(자바) (0) | 2021.09.30 |
[알고리즘/백준] 9012 괄호(자바, 스택) (0) | 2021.09.30 |
댓글