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
排序题可以用sort()函数解决,本题的关键在于如果分数一样,那么排名也要相同,
但接下来的人排名要顺延,比如a 98;b 98 ;c 97,刚开始写算法吧a,b,c排名输出为1 1 2,而不是1 1 3,以后别犯这么简单的失误了!!!
输出顺序是分数由大到小,分数一样看id,id的字典序小的则先输出,所以id用string来定义,这样可以直接比较字典序。
#include <iostream> #include<stdio.h> #include<string> #include<algorithm> using namespace std; struct student{ string id; int scroce; int rk;//记录总排名 int room; int room_rk;//记录房间排名 }; bool cmp(student a,student b){ if(a.scroce!=b.scroce) return a.scroce>b.scroce; else return a.id<b.id;//string直接字典序比较,不用char里的strcmp; } int main() { student stu[99999];//反正内存64M不如大胆点 int n,i,s,t,a,b,x; cin>>n; a=b=0; for(i=0;i<n;i++){ cin>>s; for(t=0;t<s;t++){ cin>>stu[a].id>>stu[a].scroce; stu[a].room=i+1; a++;//a为总人数。 } sort(stu+b,stu+a,cmp);//(sub+b,stu+a)为一个房间内的考生。 stu[b].room_rk=1; for(x=b+1;x<a;x++){ if(stu[x].scroce==stu[x-1].scroce) stu[x].room_rk=stu[x-1].room_rk;//分数相同,则排名一致。 else stu[x].room_rk=x+1-b; } b=a; } sort(stu,stu+a,cmp);//总的考生再排序一遍。 int r=1; stu[0].rk=1; for(i=1;i<a;i++){ if(stu[i].scroce==stu[i-1].scroce) stu[i].rk=stu[i-1].rk; else stu[i].rk=i+1; } cout<<a<<endl; for(i=0;i<a;i++){ cout<<stu[i].id<<" "<<stu[i].rk<<" "<<stu[i].room<<" "<<stu[i].room_rk<<endl; } }
原文地址:https://www.cnblogs.com/kalicener/p/12425969.html
- rpc框架之 avro 学习 2 - 高效的序列化
- rpc框架之HA/负载均衡构架设计
- 使用Docker-Docker for Web Developers(2)
- 打造高效前端工作环境-tmuxinator
- 在Linux Mint上安装node.js和npm
- JS魔法堂:再识Number type
- (cljs/run-at (JSVM. :browser) "搭建刚好可用的开发环境!")
- (cljs/run-at (->JSVM :browser) "语言基础")
- 微博爬虫
- 电话域名受欢迎,微语言融资3000万
- 前端魔法堂——异常不仅仅是try/catch
- (cljs/run-at (JSVM. :all) "一起实现柯里化")
- (cljs/run-at (JSVM. :browser) "简单类型可不简单啊~")
- 前端魔法堂:解秘FOUC
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- MYSQL logstash 同步数据到es的几种方案对比以及每种方案数据丢失原因分析。
- 手写“SpringBoot”:几十行代码基于Netty搭建一个 HTTP Server
- SpringCloud Sleuth 分布式请求链路追踪
- StarUML 使用方法
- nacos 服务注册与配置中心
- sql语句中(+)的作用
- 1.7 C++运算符
- 探花交友_搭建开发环境
- 编程体系结构(04):JavaIO流文件管理
- Hadoop框架:HDFS简介与Shell管理命令
- OpenCV的Mat类型以及基本函数使用
- Hadoop框架:HDFS读写机制与API详解
- 编程体系结构(06):Java面向对象
- RabbitMQ在分布式系统中的应用
- spring5新特性