1. 考生的编号是数字字符串,但是没必要转化成整数再比较,可以直接用strcmp()
2. 对整体的排名进行编号时所有信息都已经齐备,可以边编号边输出
3. 需要有些思量的地方是部分编号,当当前学生的分数不等于他上一个该怎么办
4. 真的需要int final_rank这个属性吗?可以不用的,因为它的输出是紧接着得到
5. 在用scanf读入的时候千万注意除了字符数组都要加&,否则容易出现少读或者段错误
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;const int maxn = 100*300;struct stud{char reg_no[15] = "";int score = 0;int location = 0;int local_rank = 0;
}studs[maxn];//分数高的上去,然后是注册号小的上去
bool cmp(stud a,stud b){if(a.score!=b.score) return a.score>b.score;else return strcmp(a.reg_no,b.reg_no)<0;
}int main(){int n;scanf("%d",&n);int idx = 0;//整张列表for(int i=1;i<=n;i++){int k;scanf("%d",&k);for(int j=0;j<k;j++){int temp =idx++;scanf("%s %d",studs[temp].reg_no,&studs[temp].score);studs[temp].location = i;}//本地排序 sort(studs+idx-k,studs+idx,cmp);//本地编号 studs[idx-k].local_rank = 1;for(int j=idx-k+1;j<idx;j++){if(studs[j].score!=studs[j-1].score)studs[j].local_rank = j-idx+k+1;else studs[j].local_rank = studs[j-1].local_rank;}}//整体排序 sort(studs,studs+idx,cmp);//输出总人数 printf("%d\n",idx); //整体编号+输出int r = 1;//总排名,当当前学生分数不等于上一个,则更新rfor(int i=0;i<idx;i++){if(i>0&&studs[i].score!=studs[i-1].score)r = i+1;printf("%s %d %d %d\n",studs[i].reg_no,r,studs[i].location,studs[i].local_rank); } return 0;
}