PAT A1025
- 多个地方人同时考试,要给每个考点排序,还要总体排序
- 先在读取单个地点以后就排序然后给rank
- 注意排序时,如与前面分数同,那么rank同,如不同,就是当前下标,不是前一个的rank加一哦,前面也许有并列的
- strcmp(a.id,b.id)会在a比b大的时候大于0,相等等于0,小于时返回负数
- 注意sort(a,a+num,cmp)别忘了写数组名,手误第一次只写了下标,sort会返回让cmp为true的顺序
- 头文件#include<string.h>才是处理字符数组用到的各类头文件,#include<string>是对string类的,在做题过程中使用较少的
- 读取%s,后面对字符数组,会遇到空格就停了,如果有%d读取int到行末尾,后面%s,那么会自动从下一行开始读取的。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct student{ char id[15]; int local_num; int local_rank; int final_rank; int score; }stu[30005]; bool cmp(student a,student b) { if(a.score!=b.score) return a.score>b.score; return strcmp(a.id,b.id)<0; } int local[105]; int nlocal,nstudent=0;// int main() { memset(local,0,sizeof(local)); scanf("%d",&nlocal); for(int i=1;i<=nlocal;i++) { scanf("%d",&local[i]); for(int j=0;j<local[i];j++) { scanf("%s %d",stu[nstudent+j].id,&stu[nstudent+j].score); stu[nstudent+j].local_num=i; } sort(stu+nstudent,stu+nstudent+local[i],cmp); stu[nstudent].local_rank=1; for(int j=1;j<local[i];j++) { if(stu[nstudent+j].score==stu[nstudent+j-1].score) stu[nstudent+j].local_rank=stu[nstudent+j-1].local_rank; else stu[nstudent+j].local_rank=j+1; } /*for(int j=0;j<local[i];j++) printf("%s %d %d ",stu[nstudent+j].id,stu[nstudent+j].local_num,stu[nstudent+j].local_rank);*/ nstudent+=local[i]; } printf("%d ",nstudent); sort(stu,stu+nstudent,cmp); stu[0].final_rank=1; for(int i=1;i<nstudent;i++) { if(stu[i].score!=stu[i-1].score) stu[i].final_rank=i+1; else stu[i].final_rank=stu[i-1].final_rank; } for(int i=0;i<nstudent;i++) printf("%s %d %d %d ",stu[i].id,stu[i].final_rank,stu[i].local_num,stu[i].local_rank); return 0; }