백준 알고리즘 5635번 : 생일

Zoo Da·2021년 5월 20일
0

백준 알고리즘

목록 보기
59/337
post-thumbnail

링크

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

문제

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

입력

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

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

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

출력

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

예제 입력 및 출력

풀이 코드

// 5635번 : 생일

#include <stdio.h>
#include <stdlib.h>

typedef struct{
  char name[16];
  int day;
  int month;
  int year;
} Birthday;

int compare(const void *a,const void *b){
  Birthday A = *(Birthday*)a;
  Birthday B = *(Birthday*)b;
  if(A.year > B.year){
    return 1;
  }
  else if(A.year == B.year){
    if(A.month > B.month){
      return 1;
    }
    else{
      return -1;
    }
  }
  else{
    return -1;
  }
  return 0;
}

int main(){
  int test;
  scanf("%d",&test);
  Birthday *list;
  list = (Birthday *)malloc(sizeof(Birthday) * test);
  for(int i = 0; i < test; i++){
    scanf("%s %d %d %d",list[i].name,&list[i].day,&list[i].month,&list[i].year);
  }
  qsort(list,test,sizeof(Birthday),compare);
  printf("%s\n%s",list[test -1].name,list[0].name);
  free(list);
  return 0;
}
profile
메모장 겸 블로그

0개의 댓글