1025 PAT Ranking (25分) 思路分析 +满分代码
题目
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.
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个考场,给出每个考场内的考生数目m、考生的编号、分数,让最终输出考生的最终排名,输出格式是:编号 总排名 考场号 考场内排名,要求排名是按照分数从高到底到底,如果分数一样就并列,但是要学号小的在前。
注意点:
- 学生编号是 13 位数字,最后输出也一定要是13位,不足13位补前导0。
- 假如 a b 分数相同,之后是 c d分数相同,排名是 1 1 3 3,而不是 1 1 2 2,也就是说,第二个人他把那个位置占了,但是排名要和他前面那个人保持一致。
思路分析
- 构建按结构体
Student存储考试信息:因为编号是13位数字,所以不能用int,可以用long long或者char[]或者string,但因为编号也要用于排序,字符串数组排序需要逐个比较字符,速度较慢,所以我们直接用long long存储了,直接比大小 - 用
vector<Student> total_stu保存全部学生信息,用vector<Student> local_stu保存考场内学生信息。 - 考场号按顺序编号
1-N,没读入一个考场的学生信息,就先按分数排名,得到考场内排名stu[i].local_rank,注意分数相同排名相同,然后将其移入 total_stu。 - 多个考场考生信息全部统计完后,对
total_stu进行排序,得到 每个考生的最终排名,同样需要调整分数相同排名相同。 - 最后,输出考生数目(
total_stu.size()),输出每个考生信息。
满分代码
注意输出编号一定要是 printf("%013lld", stu[i].no),不然你最后一个用例过不了。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// 考生信息
struct Student {
// 编号 13位数字
// string no; // 字符串排名需要逐个比较字符,速度慢,这里用空间换时间
long long no;
// 分数 最终排名 考场号 考场内排名
int score, final_rank, local_number, local_rank;
};
// 按分数降序
bool cmp(Student a, Student b) {
return a.score != b.score ? a.score > b.score : a.no < b.no;
}
int main() {
// N个考场,考场内M个考生
int n, m;
cin >> n;
// 保存全部考生信息
vector<Student> total_stu;
for (int i = 1; i <= n; ++i) {
// 当前考场 m 人
cin >> m;
// 保存当前考场考生信息
vector<Student> local_stu(m);
for (int j = 0; j < m; ++j) {
// 学号和分数
cin >> local_stu[j].no >> local_stu[j].score;
// 当前考场号
local_stu[j].local_number = i;
}
// 考场内按分数排名
sort(local_stu.begin(), local_stu.end(), cmp);
// 设置考场内排名,成绩相同排名应该一样,
// 再将当前考场内考生信息加入全部信息集合
local_stu[0].local_rank = 1;
total_stu.push_back(local_stu[0]);
for (int j = 1; j < m; ++j) {
// 并列
if (local_stu[j].score == local_stu[j - 1].score)
local_stu[j].local_rank = local_stu[j - 1].local_rank;
// 注意这里是 j + 1,不是 local_stu[j - 1].local_rank + 1
// 因为排名是 1 1 3 4 4 6 这样的规则
else
local_stu[j].local_rank = j + 1;
// 将他加入全部信息的集合
total_stu.push_back(local_stu[j]);
}
}
// 再对全部学生按成绩降序排名,得到考生的最终排名
sort(total_stu.begin(), total_stu.end(), cmp);
// 设置最终排名
total_stu[0].final_rank = 1;
for (int j = 1; j < total_stu.size(); ++j) {
// 并列
if (total_stu[j].score == total_stu[j - 1].score)
total_stu[j].final_rank = total_stu[j - 1].final_rank;
else
total_stu[j].final_rank = j + 1;
}
// 输出全部考生数目
cout << total_stu.size() << endl;
// 输出每个考生的 编号 总排名 考场号 考场内排名
// 注意编号要13位输出
for(int i = 0; i < total_stu.size(); i++)
printf("%013lld %d %d %d\n", total_stu[i].no, total_stu[i].final_rank, total_stu[i].local_number, total_stu[i].local_rank);
return 0;
}
1025 PAT Ranking (25分) 思路分析 +满分代码的更多相关文章
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 1025 PAT Ranking (25分)
1025 PAT Ranking (25分) 1. 题目 2. 思路 设置结构体, 先对每一个local排序,再整合后排序 3. 注意点 整体排序时注意如果分数相同的情况下还要按照编号排序 4. 代码 ...
- PAT甲级:1025 PAT Ranking (25分)
PAT甲级:1025 PAT Ranking (25分) 题干 Programming Ability Test (PAT) is organized by the College of Comput ...
- 1018 Public Bike Management (30分) 思路分析 + 满分代码
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
- 1025 PAT Ranking (25 分)
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...
- PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)
题意: 给定一次PAT测试的成绩,要求输出考生的编号,总排名,考场编号以及考场排名. 分析: 题意很简单嘛,一开始上来就,一组组输入,一组组排序并记录组内排名,然后再来个总排序并算总排名,结果发现最后 ...
- 【PAT甲级】1025 PAT Ranking (25 分)(结构体排序,MAP<string,int>映射)
题意: 输入一个正整数N(N<=100),表示接下来有N组数据.每组数据先输入一个正整数M(M<=300),表示有300名考生,接下来M行每行输入一个考生的ID和分数,ID由13位整数组成 ...
- 【PAT】1025. PAT Ranking (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1025 题目描述: Programming Ability Test (PAT) is orga ...
- 1025. PAT Ranking (25)
题目如下: Programming Ability Test (PAT) is organized by the College of Computer Science and Technology ...
随机推荐
- Android App安全渗透测试(一)
一. 实验环境搭建 1. 安装JDK 2. 安装Android Studio 3. 模拟器或真机 我的是夜神模拟器和nexus 工具 Apktool ...
- Vue【你知道吗?】
前言 Vue的由来 Vue最早发布于2014年左右,作者是美中国学生尤雨溪.Vue 的定位就是为前端开发提供一个低门槛,高效率,但同时又能够伴随用户成长的框架 尤雨溪谈Vue.js :缔造自由与真我 ...
- 树莓派3b在rt-thread上移植LittlevGL
树莓派3b在rt-thread上移植LittlevGL 目录 树莓派3b在rt-thread上移植LittlevGL 1.本文概述 2.资源准备 3.上手体验 4.rt-thread与lvgl进行无缝 ...
- Web前端三大主流框架是什么?Web前端前景与就业形势
近十年以来,IT行业发展火热,衍生了很多新职业,例如UI设计师.开发工程师.软件测试工程师等等,在众多备受瞩目的新生职业中,Web前端工程师是其中的一员.那么Web前端三大主流框架是什么呢? 一.We ...
- 2020最新的web前端体系和路线图,想学web前端又不知道从哪开始的快来瞧一瞧呀
web前端其实是相对于服务器语言是简单的,并且对于初学者是非常友好的,因为在前期学习能够看到很好的效果.但是他的路线 也就是学习体系不成熟,所以导致很多初学者不知道怎么学?下面我就讲讲web前端的体系 ...
- (四)PL/SQL运算符
运算符是一个符号,告诉编译器执行特定的数学或逻辑操作. PL/SQL语言有丰富的内置运算符,运算符提供的以下几种类型: 1.算术运算符 2.关系运算符 3.比较运算符 4.逻辑运算符 5.字符串运算符 ...
- [Inno Setup] Do not show application version in “Program and Features” control panel
Set AppVersion empty. But, then you have to set the AppVerName. Depending on your needs either set i ...
- 《例说51单片机(C语言版)(第3版)》——1-3 认识MCS-51的存储器结构
本节书摘来异步社区<例说51单片机(C语言版)(第3版)>一书中的第1章,第1.3节,作者:张义和,王敏男,许宏昌,余春长,更多章节内容可以访问云栖社区"异步社区"公众 ...
- JAVA编程思想 Ch3.6题
练习6:在练习5的基础上,创建一个新的Dog索引,并对其赋值为Spot对象.测试用==和equals()方法来比较引用结果. public class quan { String name; Stri ...
- 数论--HDU 1495 非常可乐
Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和se ...