๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/11609)
< Class Time >
๋ฌธ์ ํ์ด
first, last name ์์ผ๋ก ์ ๋ ฅ๋ฐ์ last, first ์์ผ๋ก ์ ๋ ฌํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class _11609_ { // Class Time
static class Name implements Comparable<Name> {
private String first;
private String last;
public Name(String first, String last) {
this.first = first;
this.last = last;
}
@Override
public int compareTo(Name o) {
if (this.last.compareTo(o.last) == 0) {
return this.first.compareTo(o.first);
}
return this.last.compareTo(o.last);
}
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(bf.readLine());
Name[] arr = new Name[n];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(bf.readLine());
arr[i] = new Name(st.nextToken(), st.nextToken());
}
Arrays.sort(arr);
for (int i = 0; i < n; i++) {
System.out.println(arr[i].first + " " + arr[i].last);
}
}
}
๋ณ์)
n : ํ์ ์
arr : ํ์ ์ด๋ฆ ์ ์ฅํ๋ ๋ฐฐ์ด
Name
first, last ์ด๋ฆ์ ๋ณ์๋ก ๊ฐ์ง๋ฉฐ last, first ์์ผ๋ก ์ ๋ ฌํ๋ค.
Main
ํ์ ์ n์ ์ ๋ ฅ๋ฐ๋๋ค. ํ์ ์๋งํผ ํ์ ์ด๋ฆ์ ์ ๋ ฅ๋ฐ์ ๋ฐฐ์ด์ ์ ์ฅํ ํ ์ ๋ ฌํ์ฌ ์ถ๋ ฅํ๋ค.

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 7774_์ฝ์ผํธ (0) | 2024.11.05 |
---|---|
[Baekjoon] 11235_Polling (0) | 2024.11.04 |
[Baekjoon] 6177_Statistics (0) | 2024.10.30 |
[Baekjoon] 6160_Election Time (0) | 2024.10.29 |
[Baekjoon] 10527_Judging Troubles (0) | 2024.10.28 |