백준/정렬

5635 생일

have a good time 2021. 10. 27. 21:37

https://www.acmicpc.net/problem/5635

 

5635번: 생일

어떤 반에 있는 학생들의 생일이 주어졌을 때, 가장 나이가 적은 사람과 가장 많은 사람을 구하는 프로그램을 작성하시오.

www.acmicpc.net

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.io.IOException;

 
public class Main {
	
	
	
	public static void main(String[] args) throws IOException {
	
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
	
		String student [][] = new String [N][4];
		
		
		for(int i=0; i<N;i++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			for(int j=0;j<4;j++) {
				student[i][j] = st.nextToken();
			}
		}
	
		Arrays.sort(student, new Comparator<String[]>() {
			
			@Override
			public int compare(String[] a, String[] b) {
				if(Integer.parseInt(a[3])==Integer.parseInt(b[3])) {
					if(Integer.parseInt(a[2])==Integer.parseInt(b[2])) {
						return Integer.parseInt(a[1]) - Integer.parseInt(b[1]);
					}else {
						return Integer.parseInt(a[2]) - Integer.parseInt(b[2]);
					}
						
					}return Integer.parseInt(a[3]) - Integer.parseInt(b[3]);
				}
			
			
		});
		
		System.out.print(student[N-1][0]+"\n"+student[0][0]);
	
		}
		}

 

Arrays.sort에서 compare을 override해서 정렬할 수 있다.

 

compare() 메서드는, 

 

예를 들어서

compare(A,B)에서

A<B : 음수리턴

A==B : 0리턴

A>B : 양수리턴

한다.

 

그런데 양수를 리턴하면, 두 객체의 자리가 변경된다.

즉,

A B 로 정렬된 상태에서 

compare() 메서드 리턴 값이 양수라면

B A 로 자리가 바뀌는 것이다.

 

참고 자료 : https://velog.io/@injoon2019/%EC%9E%90%EB%B0%94-Comparator%EC%99%80-Comparable

 

 

 

따라서 Arrays.sort(student, new Comparator<String[]>() 을 사용하여 

정렬하게 되면, 

 

예제 입력1 에 나온 값으로 따져보겠다.

 

Mickey 1 10 1991

Alice 30 12 1990

Tom 15 8 1993

Jerry 18 9 1990

Garfield 20 9 1990

 

일단 생년부터 큰 수를 따져야 해서, 

 

Mickey 1 10 1991

Alice 30 12 1990

이 값을 비교한다고 하면, (Mickey, Alice를 따져볼 때)

1991 1990 으로 우선 정렬된 상태이다.

 

여기서 1991>1990 이므로 compare 메서드에서 양수가 리턴될 것이다.

그렇다면 순서가 바뀌므로 1990 1991 이 되게 된다.

 

이런식으로 생년, 월, 일 순으로 따져나간다.

 

if(Integer.parseInt(a[3])==Integer.parseInt(b[3])) { : 생년이 같다면
if(Integer.parseInt(a[2])==Integer.parseInt(b[2])) { : 월이 같다면


return Integer.parseInt(a[1]) - Integer.parseInt(b[1]); : 1) 생년이 같고 월이 같으면 일을 비교
}else {
return Integer.parseInt(a[2]) - Integer.parseInt(b[2]); : 2) 생년이 같다면 월을 비교
}

}return Integer.parseInt(a[3]) - Integer.parseInt(b[3]); 3) 생년을 비교 

 

 

 

 

 

참고 자료 :

 

https://airzinc.tistory.com/entry/%EB%B0%B1%EC%A4%80-5635-%EC%83%9D%EC%9D%BC-JAVA?category=872364 

 

백준 5635 생일 (JAVA)

https://www.acmicpc.net/problem/5635 5635번: 생일 어떤 반에 있는 학생들의 생일이 주어졌을 때, 가장 나이가 적은 사람과 가장 많은 사람을 구하는 프로그램을 작성하시오. www.acmicpc.net Arrays.sort( 배열..

airzinc.tistory.com

 

https://st-lab.tistory.com/113?category=857114 

 

[백준] 10814번 : 나이순 정렬 - JAVA [자바]

www.acmicpc.net/problem/10814 10814번: 나이순 정렬 온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람

st-lab.tistory.com