[BOJ] 백준 #5635. 생일 (C++)

최대 1 분 소요

🎨 문제

boj-5635

문제 링크: https://www.acmicpc.net/problem/5635

  • 알고리즘 분류: 구현, 정렬
  • 난이도: Silver Ⅴ



💬 풀이

  1. [이름, 생일 정보(dd,mm,yyyy)를 담은 구조체] & [구조체 변수 n개를 담은 벡터]
  2. 생일(나이) 순으로 sort()
    👉🏻 cmp() 짤 때, true인게 먼저 온다



👩‍💻 코드

C++

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef struct {
	string name;
	int d, m, y;
}birthday;

bool cmp(const birthday &a, const birthday &b) {
	// 나이 내림차순
	if (a.y == b.y) {
		if (a.m == b.m) {
			return a.d < b.d;
		}
		else { return a.m < b.m; }
	}
	else { return a.y < b.y; }
}

int main() {
	//freopen("input.txt", "rt", stdin);
	int n;
	cin >> n;
	vector<birthday> v;
	for (int i = 0; i < n; i++) {
		string s;
		int x, y, z;
		cin >> s >> x >> y >> z;
		v.push_back({ s,x,y,z });
	}

	sort(v.begin(), v.end(), cmp);
	
	cout << v.back().name << endl << v.front().name;

	return 0;
}


댓글남기기