自定义排序 而且要循环输入
成绩排序
http://www.nowcoder.com/questionTerminal/0383714a1bb749499050d2e0610418b1
include
include
include
include<string.h>
using namespace std;
typedef struct Student {
char name[50];
int score;
int n;
}stu;
bool compare1(stu A, stu B) {
if(A.score==B.score)
return A.n<B.n;
else
return A.score > B.score;
}
bool compare2(stu A, stu B) {
if(A.score==B.score)
return A.n<B.n;
else
return A.score < B.score;
}
stu s[100];
int main() {
int method, n;
while(scanf("%d\n%d", &n, &method)!=EOF){
for (int i = 0; i < n; i++) {
scanf("%s %d ", s[i].name, &s[i].score);
s[i].n=i;
}
if (method == 0)
sort(s, s + n, compare1);
else
sort(s, s + n, compare2);
for (int i = 0; i < n; i++) {
printf("%s %d\n", s[i].name, s[i].score);
}
}
return 0;}


华为工作强度 1292人发布