๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11609_Class Time

๋ฟŒ์•ผ._. 2024. 11. 1. 00:14
๋ฌธ์ œ(์ถœ์ฒ˜: 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