当前位置: 代码迷 >> 综合 >> PAT甲级题目1025 PAT Ranking
  详细解决方案

PAT甲级题目1025 PAT Ranking

热度:8   发布时间:2024-01-30 02:39:42.0

题目大意:
simultaneously(同时地) generate(形成)
程序设计能力竞赛被浙江大学的计算机科学与技术学院举办。每次比赛在不同的地方同时进行,比赛后排名表会被立即合并。现在你的任务是写一个程序正确地合并所有的成绩表得出最后排名。

输入:
①第一行,一个正数N:考场的数量
②剩下N个块,每个块有一个K,表示这个考场K个考生成绩

输出:
考生编号 最终排名 所在考场编号 所在考场排名

思路:
用一个结构体表示一个考生(编号,成绩,所在考场编号,所在考场排名,成绩最终排名),每读取一个考场的考生信息,更新每个考生在这个考场的排名,最后全部读取完,得出最终排名

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Student{char id[15];//考生编号int score;//考生成绩int location_number;//考生所在考场排名int local_rank;//考生最终排名
}stu[30010];//考生数量最大为300*100
bool cmp(Student a,Student b)//分数越高排名越靠前,分数相同的按编号小的排名在前
{if(a.score!=b.score){return a.score>b.score;}elsereturn strcmp(a.id,b.id)<0;
}
int main()
{int n,k,num=0;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&k);for(int j=0;j<k;j++)//读取一个考场的考生成绩信息{scanf("%s %d",stu[num].id,&stu[num].score);stu[num].location_number=i;num++;	}	sort(stu+num-k,stu+num,cmp);//先在本考场排序,这个考场第一个考生在结构体中的下标为num-k,最后一个为num-1stu[num-k].local_rank=1;//排序后第一个考生排名为1for(int j=num-k+1;j<num;j++)//得出所在考场排名{if(stu[j].score==stu[j-1].score){stu[j].local_rank=stu[j-1].local_rank;//成绩相同,排名相同}else{stu[j].local_rank=j+1-(num-k);//成绩不同,第j个考生,排名为(j+1-(num-k))}}}	printf("%d\n",num);sort(stu,stu+num,cmp);int r=1;for(int i=0;i<num;i++){if(i>0&&stu[i].score!=stu[i-1].score){r=i+1;}printf("%s ",stu[i].id);printf("%d %d %d\n",r,stu[i].location_number,stu[i].local_rank);}return 0;
}