🌞Algorithm/🔥Baekjoon

[Baekjoon] 8633_Sortowanie biżuterii

뿌야._. 2024. 12. 11. 17:14
문제(출처: https://www.acmicpc.net/problem/8633)

< Sortowanie biżuterii >

 

문제 풀이 

 

문자열 길이가 짧은 순, 길이가 같다면 사전순으로 정렬한다.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Comparator;

public class _8633_ { // Sortowanie biżuterii

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
		int n = Integer.parseInt(bf.readLine());
        
		String arr[] = new String[n];
		for (int i = 0; i < n; i++) {
			arr[i] = bf.readLine();
		}
        
		Arrays.sort(arr, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				if (o1.length() == o2.length()) {
					return o1.compareTo(o2);
				}
				return o1.length() - o2.length();
			}
		});
        
		for (int i = 0; i < n; i++) {
			bw.write(arr[i] + "\n");
		}
		bw.flush();
	}
}
변수)
n : 문자열 개수
arr : 문자열 배열

 

문자열 개수를 입력받는다. 문자열 개수만큼 문자열을 입력받아 arr에 저장한다. 문자열을 길이가 짧은 순으로, 길이가 같다면 사전 순으로 정렬한 후 출력한다.



 

'🌞Algorithm > 🔥Baekjoon' 카테고리의 다른 글

[Baekjoon] 13211_Passport Checking  (0) 2024.12.13
[Baekjoon] 18703_Duplicate Files  (0) 2024.12.12
[Baekjoon] 20376_Counting Monuments  (0) 2024.12.10
[Baekjoon] 11649_Xedni Drawkcab  (1) 2024.12.09
[Baekjoon] 21177_No Thanks!  (1) 2024.12.06