题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include <stdio.h>
#include <stdlib.h>
#define maxn 100
typedef struct student{
char name[maxn];
int sorce;
}student;
int compare0(const void *a,const void *b){
const struct student *pa=(const struct student *)a;
const struct student *pb=(const struct student *)b;
if(pa->sorce>=pb->sorce) return -1;
else return 1;
}
int compare1(const void *a,const void *b){
const struct student *pa=(const struct student *)a;
const struct student *pb=(const struct student *)b;
if(pb->sorce>=pa->sorce) return -1;
else return 1;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int temp;
scanf("%d",&temp);
student s[n];
for(int i=0;i<n;i++){
scanf("%s %d",&s[i].name,&s[i].sorce);
}
if(temp==0)
qsort(s,n,sizeof(struct student),compare0);
else if(temp==1)
qsort(s,n,sizeof(struct student),compare1);
for(int i=0;i<n;i++){
printf("%s %d\n",s[i].name,s[i].sorce);
}
}
return 0;
}
查看10道真题和解析