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 ...
随机推荐
- #lua中编写shader的方式
lua中编写shader的方式 1. 字符串拼接 类似于下面这种 vertDefaultSource = "\n".."\n" .. "attribu ...
- Java集合详解6:这次,从头到尾带你解读Java中的红黑树
<Java集合详解系列>是我在完成夯实Java基础篇的系列博客后准备开始写的新系列. 这些文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查 ...
- 【Kubernetes学习之二】Kubernetes集群安装
环境 centos 7 Kubernetes有三种安装方式:yum.二进制.kubeadm,这里演示kubeadm. 一.准备工作1.软件版本 软件 版本 kubernetes v1.15.3 Cen ...
- storm并行
Storm并行度 wordcount 统计job代码 public class WordCountTopology { private static final String SENTENCE_SPO ...
- 蚂蚁花呗5面面试真题,你敢来挑战一下吗?(Java岗)
蚂蚁花呗一面(一个小时): JDK 中有哪几个线程池?顺带把线程池讲了个遍 Java容器有哪些?哪些是同步容器,哪些是并发容器? ArrayList和LinkedList的插入和访问的时间复杂度? j ...
- HTTP漫谈
一.说明 1.1 当前背景说明 很多web的书包括web安全的书都会有一章介绍http协议,我就总恶意揣测作者是在凑字数,一般都直接跳过去. 相比TCP/IP这种各字段基于数值代号的协议,http这种 ...
- Hyper-V虚拟机安装Ubuntu,启动的时候会出现:Please remove the installation medium,then press ENTER
Hyper-V虚拟机安装Ubuntu成功以后,重启的时候页面会一直卡在下面,并报Please remove the installation medium,then press ENTER,这是因为启 ...
- ACM | 算法 | 快速幂
目录 快速幂 快速幂取模 矩阵快速幂 矩阵快速幂取模 HDU1005练习 快速幂 幂运算:\(x ^ n\) 根据其一般定义我们可以简单实现其非负整数情况下的函数 定义法: int Pow ( ...
- Elasticsearch 记录
查看集群运行状态 GET /_cat/health?v 响应 1573460861 16:27:41 my-application yellow 1 1 372 372 0 0 371 0 - 50. ...
- 关于C语言指针的讨论
C语言指针的讨论 1.指整的概念辨析 2.指针与一维数组 3.指针与二维数组 4.指针与动态数组 5.指针数组 6. 指整与函数,形参,返回值 先熟悉一下概念,使劲把他们记下了 变量定义 类型表示 含 ...