王道机试例题3.3 成绩排序

jack 70
peter 96
Tom 70
smith 67

peter 96
jack 70
Tom 70
smith 67

smith 67

jack 70
Tom 70
peter 96

示例1

输入

3
0
fang 90
yang 50
ning 70


输出

fang 90
ning 70
yang 50


代码

#include <iostream>
#include<stdio.h>
#include<algorithm>

using namespace std;

struct student {
string name;
int score;
};

bool compare1(student x,student y) {
if (x.score == y.score) {
return 1;
}
else {
return x.score < y.score;
}
}

bool compare2(student x, student y) {
if (x.score == y.score) {
return 1;
}
else {
return x.score > y.score;
}
}

int main()
{
int n,sorj;
while (scanf_s("%d%d", &n, &sorj) != EOF) {
student arr[100];
for (int i = 0; i < n; i++) {
cin >> arr[i].name;
scanf_s("%d", &arr[i].score);
}
if (sorj == 1) {
sort(arr, arr + n, compare1);   //升序
}
else {
sort(arr, arr + n, compare2);   //降序
}
for (int i = 0; i < n; i++) {
cout << arr[i].name << " " << arr[i].score << endl;
}
}
return 0;
}


emmm…
VS能过，牛客死活通过不了

01-29 208
10-10 283
08-14 4万+
09-21 57
10-18 1321
07-30 389
05-20
09-18
09-28