Posts 백준 5635 생일 c++
Post
Cancel

백준 5635 생일 c++

문제

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

문제해설

사람들의 생년월일이 주어지면
가장 나이가 많은 사람과 가장 어린 사람을 출력하면 된다.

문제풀이

그냥 구현문제이고
정렬을 할 때 pair를 사용해서 하기에는
코드가 너무 복잡해질 것이다.
그래서 구조체를 만들고
정렬기준을 잡아서 sort함수로 정렬했다.

처음에는 cmp 구조체로 정렬기준을 잡았는데
계속 오류가 나길래 cmp함수로 바꿨더니 문제가 없었다.

전체코드

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
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct student {
	string name;
	int day, mon, year;
};

bool cmp(const struct student& x, const struct  student& y) {
	if (x.year < y.year)
		return true;
	else if (x.year == y.year) {
		if (x.mon < y.mon)
			return true;
		else if (x.mon == y.mon) {
			if (x.day < y.day)
				return true;
			else
				return false;
		}
		else
			return false;
	}
	else
		return false;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n; cin >> n;
	vector<student>arr;
	for (int i = 0; i < n; i++) {
		string x;
		int a, b, c;
		cin >> x >> a >> b >> c;
		arr.push_back({ x,a,b,c });
	}
	sort(arr.begin(), arr.end(), cmp);
	cout << arr[n - 1].name << "\n" << arr[0].name;
	return 0;
}

JPA 영속성 컨텍스트(Persistence Context)

백준 13913 숨바꼭질 4 c++

Comments powered by Disqus.