结构体成绩排名
要求按照排名从高到低输出考生信息,包括名次 id 成绩 分数不同,按分数为序,即score大的在前,小的在后 分数相同,以id为序,即id小的在前,大的在后
测试数据
输入1
:看成绩相同是否排名相同 成绩不同排名在第几就是第几
5
1000 95
1001 100
1002 94
1006 95
1007 100
输出
:
1
1001 100
1
1007 100
3
1000 95
3 1006 95
5
1002 94
输入2
:
看id是否升序
5
1000 95
1008 100
1002 94
1006 95
1007 100
输出
:
1 1007
100
1 1008
100
3 1000 95
3 1006 95
5 1002 94
C语言代码实现:
#include<stdio.h>
struct student{
int id,score;
}stu[1001];
int main(){
int n,rank=0,i;
scanf("%d",&n);
for(i=0;i<n;i++){//结构体录入
scanf("%d%d",&stu[i].id,&stu[i].score);
}
int j;
struct student temp;
for(i=0;i<n;i++){
for(j=0;j<n-i-1;j++){
if(stu[j].score<stu[j+1].score || stu[j].score==stu[j+1].score && stu[j].id>stu[j+1].id){//成绩相同,id升序排序:从小到大排序
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
for(i=0;i<n;i++){
if(stu[i].score==stu[i-1].score)
printf("%d %d %d\n",rank,stu[i].id,stu[i].score);//若后一个成绩和前一个相等,rank不变
else{
printf("%d %d %d\n",i+1,stu[i].id,stu[i].score);//若分数不相同,直接i+1表示排名rank即可
rank=i+1;//把i+1保存在rank里
}
}
return 0;
}