코딩테스트/Baekjoon

[백준 #10825] 국영수 (C++)

동띵 2022. 2. 11. 18:47

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

 

10825번: 국영수

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

www.acmicpc.net

 

이름, 국어, 영어, 수학을 한 쌍으로 묶기 위해 구조체를 사용하였다.

student라는 구조체 안에 이름, 국어 점수, 영어 점수, 수학 점수를 넣어주어 만들었고

메인에서 학생의 수(n)를 입력받고 구조체를 동적으로 할당해주었다.

student* st = new student[n];

 

그리고 문제에 나와있는 조건에 맞게 cmp 함수를 만들어

구조체를 정렬시킬 때 cmp를 넣어서 정렬해주었다.

마지막으로 for문을 사용해 정렬된 구조체의 name을 출력해주었다.

 

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

struct student {
	string name;
	int ko;
	int en;
	int ma;
};

bool cmp(const student& s1, student& s2) {
	if (s1.ko == s2.ko) {
		if (s1.en == s2.en) {
			if (s1.ma == s2.ma) {
				return s1.name < s2.name;
			}
			return s1.ma > s2.ma;
		}
		return s1.en < s2.en;
	}
	return s1.ko > s2.ko;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n;
	cin >> n;
	student* st = new student[n];

	for (int i = 0; i < n; i++) {
		cin >> st[i].name >> st[i].ko >> st[i].en >> st[i].ma;
	}

	sort(st, st + n, cmp);

	for (int i = 0; i < n; i++) {
		cout << st[i].name << "\n";
	}
	delete[]st;
	return 0;
}