这是一道非常基础的排序题,不过在结构体下显得有点麻烦。
我们先来看题目:

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

原文大意就是,先输入一个正整数n,代表考场数,再输入n组数据,每组数据包括:
正整数k,代表该考场的考生数;
k组考生数据,分别是考生的id(13位数字)和考生的成绩。
现在要求你对每个考场的考生按成绩从高到低排名,如果分数相同,则按学号从低到高排;然后再对所有考生进行排名,排名规则同上。
输出数据格式为,先输出考生总人数,再依次输出每个考生的id、总排名,考场号,在其考场的排名。

大体思路:

对结构体中的元素进行排序很容易实现,这里着重讲解一下如何处理两个考生分数相同的情况:
如果两个考生分数相同,那么他们输出时虽然按准考证号决定输出顺序,但他们的排名必须是相同的,并且后面的同学的排名必须进行特殊变动。举个例子:

小A、小B和小C的成绩分别为90、90、7分,那么小A和小B都是第1名,且小C是第三名,而不是第二名。难点就在于如何计算出小C的排名。

这里给出一个思路,即,先将该考生数列进行排序,排序后,将第一个考生设置为第一名,然后从第二个考生开始,进行分支判定,如果这个考生的成绩和前一个考生相等,那么该考生的排名也和前一个考生相等,否则该考生的成绩就等于它数组下标+1。

清楚了这一点后,这个问题就迎刃而解了。下面是AC代码:

#include <bits/stdc++.h>
using namespace std;
struct student{char name[20];int kc_rank;int score;int location_num;
}stu[30015];
bool cmp(student a,student b)
{if(a.score!=b.score)return a.score>b.score;else return strcmp(a.name,b.name)<0;//注意这里cmp函数编写方法
}
int main(){int k,n,i,j,num=0;//n代表考场数,k代表每个考场的人数,num代表考生总人数scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&k);for(j=1;j<=k;j++){scanf("%s",stu[num].name);scanf("%d",&stu[num].score);stu[num].location_num=i;num++;}sort(stu+num-k,stu+num,cmp);//对每个考场的考生排序stu[num-k].kc_rank=1;for(j=num-k+1;j<num;j++)//处理分数相同的情况{if(stu[j].score==stu[j-1].score)stu[j].kc_rank=stu[j-1].kc_rank;else stu[j].kc_rank=j-num+k+1;}}sort(stu,stu+num,cmp);//对所有考生排序int r=1;printf("%d\n",num);for(i=0;i<num;i++){printf("%s ",stu[i].name);if(i==0)printf("%d ",r);else{if(stu[i].score==stu[i-1].score)//同上,处理分数相同的情况printf("%d ",r);else{r=i+1;printf("%d ",r);}}printf("%d ",stu[i].location_num);printf("%d\n",stu[i].kc_rank);}return 0;
}

PAT-A1025 PAT Ranking相关推荐

  1. [PAT A1025]PAT Ranking

    [PAT A1025]PAT Ranking 题目描述 1025 PAT Ranking (25 分)Programming Ability Test (PAT) is organized by th ...

  2. PAT A1141 PAT Ranking of Institutions ——昨夜西风凋碧树

    PAT A1141 PAT Ranking of Institutions 中间计算TWS时不能使用int,否则最后一个测试点不过.但是比较和输出又需要用int,在里面强转int也不给AC,无奈只好先 ...

  3. 17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼

    题目 After each PAT, the PAT Center will announce the ranking of institutions based on their students' ...

  4. 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)

    在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...

  5. 算法笔记 P103 例题:【PAT A1025】PAT Ranking

    题目链接 题目 Programming Ability Test (PAT) is organized by the College of Computer Science and Technolog ...

  6. PAT 1025 PAT Ranking题解

    大致题意: 输入所有考生的考号以及所有考生的成绩,以及考生所在的考场号. 输出格式: 考生的考场号 考生的总名次 考生所在的考场号 考生所在考场的名次 首先解决考生的排名问题,应当先对考生在本考场所取 ...

  7. PAT 1085 PAT单位排行(25)(映射、集合训练)

    1085 PAT单位排行(25 分) 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​5​​),即考生人数.随 ...

  8. PAT 1025 PAT Rankings 思路与题解

    1025 PAT Ranking 原题 思路 定义结构体,将分考场排名也纳入结构体内 id以字符数组的形式存储,输入输出均用格式控制符%s即可 化繁为简,通过下标灵活控制多个一维数组的组合.不要轻易考 ...

  9. 1040 有几个PAT(PAT乙级 C++)

    题目 字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位(T) ...

  10. PAT 1085. PAT单位排行 (25) - 乙级

    每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数N(<=105),即考生人数.随后N行,每行按下列格式给出一个考生的信 ...

最新文章

  1. AssetBundle——外部加载资源Asset
  2. 系统中编译安装ZLMediaKit的步骤
  3. vs 正则表达式替换
  4. Beta冲刺 (7/7)
  5. LAMP+LNMP(四)PHP安装实践
  6. 计算机系统军训口号,霸气的军训口号大全
  7. Verilog HDL 学习笔记3-Latch
  8. Vue 3 正式发布
  9. [Windows] 【直播放映馆V9.0】Bilibili,斗鱼,虎牙,企鹅电竞,音乐电台,无广告看电影直播!...
  10. Hough变换的理解
  11. 创建一个Scrapy爬虫项目并运行
  12. 删除magisk模块
  13. 相爱相杀:移动联通IT支撑回忆录(十四)
  14. 通过PHP访问MYSQL外文书籍,通过PHP访问MySQL外文翻译(模版2)
  15. element-plus更换主题色(有手就行)
  16. input只能输入数字0-9(不含小数点)
  17. 打字母案例完整版(C#)
  18. 计算机网络学习--协议族、协议栈
  19. 纯CSS实现粉红爱心动画
  20. fal金融科技研究院第十四期课程

热门文章

  1. 5月上海见-国际产学研专家集结第六届全球云计算大会中国站
  2. 2017黑客大预言:病毒传播无需文件,无人机可能成为炸弹
  3. Instruments-Automation: 通过命令行执行测试用例
  4. T-SQL连接查询,基础连接理解
  5. JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,ListT转JSON,JSON转ListT,JSON转C#对象...
  6. C语言里面邻接表的创建
  7. Facebook F8|闲鱼高级技术专家参会分享
  8. 记我朋友的一次前端面试
  9. 支持10秒自毁的新芯片
  10. HDU 1693 Eat the Trees ——插头DP