https://www.acmicpc.net/problem/10825
<์์ฝ>
n๋ช
์ ํ์์ ์ด๋ฆ๊ณผ ๊ตญ์ด, ์์ด, ์ํ ์ฑ์ ์ ์
๋ ฅ๋ฐ๊ณ ์๋์ ์กฐ๊ฑด์ ๊ฐ์ถฐ์ ์ ๋ ฌํ๋ ๋ฌธ์
typedef struct {
string name;
int kor;
int eng;
int math;
}student(๊ตฌ์กฐ์ฒด ์ด๋ฆ);
vector <student> score(n);
-> ๊ตฌ์กฐ์ฒด ๋ฒกํฐ๋ ์๋ฃํ์ ๊ตฌ์กฐ์ฒด ์ด๋ฆ์ผ๋ก ๋ฃ์ด์ฃผ๋ฉด ๋จ
sort(score.begin(), score.end(), compare);
bool compare(student x, student y) {
if (x.kor == y.kor) {
if (x.eng == y.eng) {
if (x.math == y.math) {
return x.name < y.name; // ์ด๋ฆ ์ค๋ฆ์ฐจ์ ์ ๋ฆฌ
}
else return x.math > y.math; // ์ํ ์ฑ์ ๋ด๋ฆผ์ฐจ์ ์ ๋ฆฌ
}
else return x.eng < y.eng; // ์์ด ์ฑ์ ์ค๋ฆ์ฐจ์ ์ ๋ฆฌ
}
else return x.kor > y.kor; // ๊ตญ์ด ์ฑ์ ๋ด๋ฆผ์ฐจ์ ์ ๋ฆฌ
}
-> a>b? ์์ true๊ฐ ๋ฐํ๋๋ฉด ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
a<b?์์ true๊ฐ ๋ฐํ๋๋ฉด ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct {
string name;
int kor;
int eng;
int math;
}student;
bool compare(student x, student y) {
if (x.kor == y.kor) {
if (x.eng == y.eng) {
if (x.math == y.math) {
return x.name < y.name;
}
else return x.math > y.math;
}
else return x.eng < y.eng;
}
else return x.kor > y.kor;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector <student> score(n);
for (int i = 0; i < n; i++) {
cin >> score[i].name >> score[i].kor >> score[i].eng >> score[i].math;
}
sort(score.begin(), score.end(), compare);
for (int i = 0; i < n; i++) {
cout << score[i].name << '\n';
}
return 0;
}