Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-5635번/Java] 생일 본문

Algorithm

[백준-5635번/Java] 생일

1984 2022. 11. 21. 17:28

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

 

5635번: 생일

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

www.acmicpc.net

 

* ArrayList / Comparator 를 이용하여 풀었다.

 

import java.io.*;
import java.util.*;

public class Main {

	public static class Person {
		String name;
		int d;
		int m;
		int y;

		public Person(String name, int d, int m, int y) {
			this.name = name;
			this.d = d;
			this.m = m;
			this.y = y;
		}

		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return this.name;
		}
	}

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		ArrayList<Person> student = new ArrayList<Person>();

		for (int i = 0; i < N; i++) {
			student.add(new Person(in.next(), in.nextInt(), in.nextInt(), in.nextInt()));
		}

		Collections.sort(student, new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				if (o1.y > o2.y || o1.y == o2.y && o1.m > o2.m || o1.y == o2.y && o1.m == o2.m && o1.d > o2.d) {
					return -1;
				} else if (o1.y == o2.y && o1.m == o2.m && o1.d == o2.d) {
					return 0;
				} else {
					return 1;
				}
			}
		});

		System.out.println(student.get(0));
		System.out.println(student.get(student.size() - 1));
	}

}
Comments