코딩로그
[백준/BOJ/C++] 5635번 생일 본문
[문제]
어떤 반에 있는 학생들의 생일이 주어졌을 때, 가장 나이가 적은 사람과 가장 많은 사람을 구하는 프로그램을 작성하시오.
[입력]
첫째 줄에 반에 있는 학생의 수 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;
}
[결과 화면]
'YJ > C++' 카테고리의 다른 글
[백준/BOJ/C++] 12778번 CTP공국으로 이민 가자 (0) | 2021.11.09 |
---|---|
[백준/BOJ/C++] 4592번 중복을 없애자 (0) | 2021.11.09 |
[백준/BOJ/C++] 2525번 오븐 시계 (0) | 2021.11.09 |
[백준/BOJ/C++] 13163번 닉네임에 갓 붙이기 (0) | 2021.11.09 |
[백준/BOJ/C++] 12605번 단어순서 뒤집기 (0) | 2021.11.09 |