백준 문제풀이/실버4

백준 10825번 - 국영수

void_melody 2022. 6. 21. 08:03

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

 

10825번: 국영수

첫째 줄에 도현이네 반의 학생의 수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 한 줄에 하나씩 각 학생의 이름, 국어, 영어, 수학 점수가 공백으로 구분해 주어진다. 점수는 1보다 크거나 같고, 1

www.acmicpc.net

이름, 국어, 영어, 수학 점수가 다 묶여야 하므로 구조체를 써야한다는 생각이 들었다.

그리고 sort 함수를 사용할 때 compare함수를 새로 구현해서 인자로 넣어주면 된다라는 생각이 들었다

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

struct student {
	string name;
	int korean, english, math;
};
bool comp(const student& s1, const student& s2)
{
	if (s1.korean == s2.korean && s1.english == s2.english && s1.math == s2.math)
	{
		return s1.name < s2.name;
	}
	if (s1.korean == s2.korean && s1.english == s2.english)
	{
		return s1.math > s2.math;
	}
	if (s1.korean == s2.korean)
	{
		return s1.english < s2.english;
	}
	return s1.korean > s2.korean;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n;
	cin >> n;
	vector<student> v(n);
	student st;
	for (int i = 0; i < n; i++)
	{
		cin >> st.name >> st.korean >> st.english >> st.math;
		v[i] = st;
	}

	sort(v.begin(), v.end(), comp);

	for (auto& i : v)
	{
		cout << i.name << '\n';
	}

	return 0;
}