1025 PAT Ranking
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.
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.
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.
- 2
- 5
- 1234567890001 95
- 1234567890005 100
- 1234567890003 95
- 1234567890002 77
- 1234567890004 85
- 4
- 1234567890013 65
- 1234567890011 25
- 1234567890014 100
- 1234567890012 85
- 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
总结:这道题不难,根据前面做过的几个差不多的结构体排序的问题,可以用差不多的方法做来,让我没有想到的是这次调试很快就调对了(很少T...T),还是挺高兴的,所以多练还是有用的!
基本思路:在一开始输入的时候将每个地区的人排名一遍,然后根据分数赋上排名,最后总的排名一边,如果分数相等,那就按序号的高低排名(从低到高)
- #include
- #include
- using namespace std;
-
- const int N=30010;
- struct Stu{
- string s;
- int score;
- int rank[3];//rank[0]表示全部排名,rank[1]表示地区,rank[2]表示当地排名
- }stu[N];
-
- bool cmp(Stu a,Stu b){
- return a.score>b.score;
- }
-
- bool cmp2(Stu a,Stu b){
- if(a.score!=b.score) return a.score>b.score;
- else return a.s
- }
-
- int main(){
- int n,k,sum=0,index=0;
- cin >> n;
-
- for(int i=0;i
- scanf("%d",&k);
- for(int j=0;j
- cin >> stu[index].s >> stu[index].score;
- stu[index++].rank[1]=i+1;
- }
- sort(stu+sum,stu+sum+k,cmp);
- stu[sum].rank[2]=1;
- for(int j=1;j
- stu[sum+j].rank[2]=j+1;
- if(stu[sum+j].score==stu[sum+j-1].score)
- stu[sum+j].rank[2]=stu[sum+j-1].rank[2];
- }
- sum+=k;
- }
-
- sort(stu,stu+sum,cmp2);
- stu[0].rank[0]=1;
- for(int j=1;j
- stu[j].rank[0]=j+1;
- if(stu[j].score==stu[j-1].score)
- stu[j].rank[0]=stu[j-1].rank[0];
- }
-
- printf("%d\n",sum);
- for(int i=0;i
- cout << stu[i].s << ' ' << stu[i].rank[0] << ' ' << stu[i].rank[1] << ' ' << stu[i].rank[2] << endl;
- }
-
- return 0;
- }
依照惯例,还是看看大佬的!
还是简洁明了,通俗易懂!(没有用cin cout 耗时少了很多)
- #include
- #include
- #include
- using namespace std;
- struct student {
- long long int no;
- int score, finrank, loca, locarank;
- };
- bool cmp1(student a, student b) {
- return a.score != b.score ? a.score > b.score : a.no < b.no;
- }
- int main() {
- int n, m;
- scanf("%d", &n);
- vector
fin; - for(int i = 1; i <= n; i++) {
- scanf("%d", &m);
- vector
v(m) ; - for(int j = 0; j < m; j++) {
- scanf("%lld %d", &v[j].no, &v[j].score);
- v[j].loca = i;
- }
- sort(v.begin(), v.end(), cmp1);
- v[0].locarank = 1;
- fin.push_back(v[0]);
- for(int j = 1; j < m; j++) {
- v[j].locarank = (v[j].score == v[j - 1].score) ? (v[j - 1].locarank) : (j + 1);
- fin.push_back(v[j]);
- }
- }
- sort(fin.begin(), fin.end(), cmp1);
- fin[0].finrank = 1;
- for(int j = 1; j < fin.size(); j++)
- fin[j].finrank = (fin[j].score == fin[j - 1].score) ? (fin[j - 1].finrank) : (j + 1);
- printf("%d\n", fin.size());
- for(int i = 0; i < fin.size(); i++)
- printf("%013lld %d %d %d\n", fin[i].no, fin[i].finrank, fin[i].loca, fin[i].locarank);
- return 0;
- }
好好学习,天天向上!
我要考研!
2022.11.2
- //思路很简单,在输入每一个地区的成绩时将地区成绩排名,然后把地区合并和在一个数组中
- //最后将数组中所有参赛者的成绩进行排名,打印结果即可
- #include
- #include
- #include
- using namespace std;
-
- struct node{
- string id;
- int f,loc,locrank,rank;
- };
- //获取排名
- bool cmp1(node a,node b){
- if(a.f!=b.f) return a.f>b.f;
- return a.id
- }
- int main(){
- int n,k,cot=0;
- scanf("%d",&n);
- vector
v[n]; -
- for(int i=0;i
- scanf("%d",&k);
- cot+=k;
- string id;
- int f;
- v[i].resize(k);
- for(int j=0;j
- cin >> id >> f;
- v[i][j]={id,f,i+1};
- }
- sort(v[i].begin(),v[i].end(),cmp1);
- int pre=-1,rank=1;
- for(int j=0;j
- if(pre!=v[i][j].f) rank=j+1;
- v[i][j].locrank=rank;
- pre=v[i][j].f;
- }
- }
- vector
s(300010) ; - int t=0,pre=-1,rank=1;
- for(int i=0;i
- for(int j=0;j
size();j++) - s[t++]=v[i][j];
- }
- printf("%d\n",t);
- sort(s.begin(),s.begin()+t,cmp1);
- for(int i=0;i
size();i++){ - if(pre!=s[i].f) rank=i+1;
- s[i].rank=rank;
- pre=s[i].f;
- }
- for(int i=0;i
- cout << s[i].id;
- printf(" %d %d %d\n",s[i].rank,s[i].loc,s[i].locrank);
- }
- return 0;
- }
-
相关阅读:
协程是如何实现线程切换的
带你10分钟学会红黑树
能链科技获评中国最具投资价值企业榜单TOP10
uafxcw.lib(afxmem.obj) : error LNK2005
C++:C++模板(函数模板与类模板的使用)
JPA与sboot整合
【每日练习】删除公共字符
Java接口(Interface)
图论27(Leetcode721账户合并)
K8s 之 ReadinessProbe(就绪探针)使用的迷惑
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/126838857
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU