본문 바로가기
카테고리 없음

백준 10814

by 씨니 2022. 3. 22.
728x90

▶10814문제 - 나이순 정렬

 

▶풀이방법

- 배열 사용 풀이방법1

import java.io.*;
import java.util.*;
public class J_10814 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int count = Integer.parseInt(br.readLine());
		String[][] arr = new String[count][2];
		
		for(int i = 0; i < count; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine() + " ");
			arr[i][0] = st.nextToken();
			arr[i][1] = st.nextToken();
		}
		
		Arrays.sort(arr, new Comparator<String[]>()  {
			@Override
			public int compare (String[] o1, String[] o2) {
					return Integer.parseInt(o1[0]) - Integer.parseInt(o2[0]);
			}
		});
		
		for(int i = 0; i < count; i++) {
			bw.write(arr[i][0] + " " + arr[i][1] + "\n");
		}
		bw.flush();
		bw.close();
		br.close();
	}
}

++ 2차원 배열 비교시, 숫자 입력되어있는 배열 칸 Integer형으로 바꿔서 비교!

 

- 리스트 사용 풀이방법 2

import java.io.*;
import java.util.*;
//ArrayList사용 풀이방법
class Member implements Comparable<Member>{
	int age;
	String name;
	
	Member(int age, String name){
		this.age = age;
		this.name = name;
	}
	
	@Override
	public int compareTo(Member m) {
		// TODO Auto-generated method stub
		if(this .age > m.age) return 1; //나이 오름차순
		else if(this .age < m.age) return -1;
		else return 0;
	}
	
}
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int count = Integer.parseInt(br.readLine());
		ArrayList<Member> list = new ArrayList<>();
		for(int i = 0; i < count; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine() + " ");
			list.add(new Member(Integer.parseInt(st.nextToken()), st.nextToken()));
		}
		Collections.sort(list); //list정렬
		
		for(int i = 0; i < count; i++) {
			bw.write(list.get(i).age + " " + list.get(i).name + "\n");
		}
		
		bw.flush();
		bw.close();
		br.close();
	}
 }
728x90