PAT 甲级 1025.PAT Ranking C++/Java
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 (≤), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤), 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.
Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
题目大意:
有N个(<= 100)考场,每个考场有K(<= 300)名考生,给出每个考场的考生的考号,分数
输出所有考生的编号,总排名,考场号,考场内排名(按照非递减顺序输出)
分析:
如果考生的分数相同,就按照编号小到大输出(考号小的优先输出)
先按照考场内对考生进行排序,将结果保存到总考生数组,再对总考生进行排序
C++实现:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std; typedef struct Student
{
string id; //考号
int score; //分数
int location; //地区
int local_rank; //地区排名
int total_rank; //总排名
}Student; bool myCmp(Student a, Student b)
{
if (a.score != b.score)
{
return a.score > b.score;
}
else
{
return a.id < b.id;
}
} int main()
{
int N; //考场数
cin >> N; int localNum = ; //考场人数
int totalNum = ; //总人数
vector<Student> totalStus; //保存所有考生的信息;
for (int i = ; i <= N; ++i)
{
cin >> localNum;
vector<Student> localStus(localNum);
for (int j = ; j < localNum; ++j)
{
cin >> localStus[j].id >> localStus[j].score;
localStus[j].location = i; //i个考场
}
sort(localStus.begin(), localStus.end(), myCmp);
localStus[].local_rank = ;
totalStus.push_back(localStus[]);
for (int j = ; j < localNum; ++j)
{
if (localStus[j].score == localStus[j - ].score)
{
//分数相同
localStus[j].local_rank = localStus[j - ].local_rank;
}
else
{
localStus[j].local_rank = j + ;
}
totalStus.push_back(localStus[j]);
}
totalNum += localNum;
} sort(totalStus.begin(), totalStus.end(), myCmp);
totalStus[].total_rank = ;
for (int i = ; i < totalNum; ++i)
{
if (totalStus[i].score == totalStus[i-].score)
{
totalStus[i].total_rank = totalStus[i - ].total_rank;
}
else
{
totalStus[i].total_rank = i + ; //注意这里的细节
}
}
cout << totalNum << endl;
for (int i = ; i < totalNum; ++i)
{
cout << totalStus[i].id << " " << totalStus[i].total_rank << " " << totalStus[i].location << " " << totalStus[i].local_rank << endl;
}
return ;
}
注意:如果分数与前一名分数相同,那么他们的排名相同。如果分数小于前一名的分数,那他的排名就是循环次数+1(因为如果前2人的排名都是1,那第i=2次循环时,第3个人的排名就是i + 1 = 3)
小结:
1. sort函数第三个参数cmp可以自己编写排序规则。
bool myCmp(Student a, Student b)
{
return a.score > b.score;
}
自定义排序规则:按照Student中的score来排序, return a.score > b.score; 按照分数从高到低排序
PAT 甲级 1025.PAT Ranking C++/Java的更多相关文章
- PAT 甲级 1025 PAT Ranking
1025. PAT Ranking (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...
- PAT甲级——1025 PAT Ranking
1025 PAT Ranking Programming Ability Test (PAT) is organized by the College of Computer Science and ...
- PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)
题意: 给定一次PAT测试的成绩,要求输出考生的编号,总排名,考场编号以及考场排名. 分析: 题意很简单嘛,一开始上来就,一组组输入,一组组排序并记录组内排名,然后再来个总排序并算总排名,结果发现最后 ...
- 【PAT】1025. PAT Ranking (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1025 题目描述: Programming Ability Test (PAT) is orga ...
- PAT 甲级 1141 PAT Ranking of Institutions
https://pintia.cn/problem-sets/994805342720868352/problems/994805344222429184 After each PAT, the PA ...
- PAT甲级——A1025 PAT Ranking
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...
- PAT甲级1075 PAT Judge
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805393241260032 题意: 有m次OJ提交记录,总共有k道 ...
- PAT 甲级 1075 PAT Judge (25分)(较简单,注意细节)
1075 PAT Judge (25分) The ranklist of PAT is generated from the status list, which shows the scores ...
- PAT甲级——A1075 PAT Judge
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
随机推荐
- 关于“100g文件全是数组,取最大的100个数”解决方法汇总
原题如下: 有一个100G大小的文件里存的全是数字,并且每个数字见用逗号隔开.现在在这一大堆数字中找出100个最大的数出来. 我认为,首先要摸清考官的意图.是想问你os方面的知识,还是算法,或者数据结 ...
- YYCache 的整体架构类图

- storm单机运行报错 ERROR backtype.storm.daemon.executor -
单机本地运行storm报错: 错误如下: java.lang.NullPointerException: null at test2.Spot2.nextTuple(Spot2.java:) ~[cl ...
- DFS or BFS --- 连通块
Oil Deposits Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 Descrip ...
- 『线段树及扫描线算法 Atlantis』
入门看这边『线段树 Segment Tree』. 扫描线 扫描线是一种解决一类平面内统计问题的算法,通常会借助线段树来实现,我们通过一道例题来引入这个算法. Atlantis Description ...
- 彻底搞懂etcd raft选举、数据同步
etcd raft选举机制 etcd 是一个分布式的k/V存储系统.核心使用了RAFT分布式一致性协议.一致性这个概念,它是指多个服务器在状态达成一致,但是在一个分布式系统中,因为各种意外可能,有的服 ...
- [Windows] - Windows/Office纯绿色一键激活工具及方法
瘟到死网上有很多一件键激活工具(如KMS),但许多带毒或报毒.这里给出一个纯绿色命令行一键激活,及自已搭建激活服务器的方法. KMS现在算法都是公开的了,可以自行在网上找到,这里不详述. 使用命令行一 ...
- soup.select的用法
1.通过标签选择 # 选择所有title标签 soup.select("title") # 选择所有p标签中的第三个标签 soup.select("p:nth-of-ty ...
- Python删除列表元素的3种方法
之前看教程的时候比较着急,对这些基础掌握不好,过来回顾一下 使用del语句删除 lis = [1, 2, 3, 'a', 'b'] print(lis) del lis[0] print(lis) 输 ...
- Java自学-数字与字符串 装箱和拆箱
Java中基本类型的装箱和拆箱 步骤 1 : 封装类 所有的基本类型,都有对应的类类型 比如int对应的类是Integer 这种类就叫做封装类 package digit; public class ...