Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
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
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 5635번 생일 본문

YJ/C++

[백준/BOJ/C++] 5635번 생일

Team DAON 2021. 11. 9. 16:29

[문제]

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

[입력]

첫째 줄에 반에 있는 학생의 수 n이 주어진다. (1 ≤ n ≤ 100)

다음 n개 줄에는 각 학생의 이름과 생일이 "이름 dd mm yyyy"와 같은 형식으로 주어진다. 이름은 그 학생의 이름이며, 최대 15글자로 이루어져 있다. dd mm yyyy는 생일 일, 월, 연도이다. (1990 ≤ yyyy ≤ 2010, 1 ≤ mm ≤ 12, 1 ≤ dd ≤ 31) 주어지는 생일은 올바른 날짜이며, 연, 월 일은 0으로 시작하지 않는다.

이름이 같거나, 생일이 같은 사람은 없다.

[출력]

첫째 줄에 가장 나이가 적은 사람의 이름, 둘째 줄에 가장 나이가 많은 사람 이름을 출력한다.

[Source Code]

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;


int main() {
	int test;
	cin >> test;

	int bigday, bigmonth, bigyear, smallday, smallmonth, smallyear;
	string bigname, smallname;

	cin >> bigname >> bigday >> bigmonth >> bigyear;
	smallname = bigname;
	smallday = bigday;
	smallmonth = bigmonth;
	smallyear = bigyear;

	for(int i = 1; i < test; i++){
		string input;
		int inputday, inputmonth, inputyear, check = 0;
		cin >> input >> inputday >> inputmonth >> inputyear;

		if(inputyear < bigyear){
			check = 1;
		}
		else if(inputyear == bigyear){
			if(inputmonth < bigmonth)
				check = 1;
			else if(inputmonth == bigmonth){
				if(inputday <= bigday)
					check = 1;
			}
		}

		if(inputyear > smallyear)
			check = 2;
		else if(inputyear == smallyear){
			if(inputmonth > smallmonth)
				check = 2;
			else if(inputmonth == smallmonth){
				if(inputday >= smallday)
					check = 2;
			}
		}

		if(check == 1){
			bigname = input;
			bigday = inputday;
			bigmonth = inputmonth;
			bigyear = inputyear;
		}else if(check == 2){
			smallname = input;
			smallday = inputday;
			smallmonth = inputmonth;
			smallyear = inputyear;
		}
	}
	cout << smallname << "\n" << bigname;
}

[결과 화면]