PTA-1025——PAT Ranking

Posted orangecyh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA-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.

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

分析:

快排、复习了一下vector

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 using namespace std;
 5 int n;
 6 int testeeNum[101];
 7 struct Testee{
 8     string id;        //考生id 
 9     int location;    //考场号 
10     int score;        //得分 
11     int localRank;    //区域排名 
12     int finalRank;    //总排名 
13 };
14 vector<Testee> testees[101];    //区域考场学生的集合
15 vector<Testee> totalTestees;    //总学生的集合 
16 
17 bool cmp(Testee a,Testee b){    //根据成绩排序 
18     if(a.score>b.score||(a.score==b.score&&a.id<b.id)){
19         return true;
20     }else{
21         return false;
22     }
23 }
24 
25 int main(){
26     cin>>n;
27     for(int i=1;i<=n;i++){
28         cin>>testeeNum[i];
29         for(int j=0;j<testeeNum[i];j++){
30             Testee testee;
31             cin>>testee.id>>testee.score;
32             testee.location=i;
33             testees[i].push_back(testee);
34         }
35         sort(testees[i].begin(),testees[i].end(),cmp);    //每个区域都进行排序
36         int k=1;
37         for(vector<Testee>::iterator it=testees[i].begin();it!=testees[i].end();it++){    //设置区域名次 
38             if(it!=testees[i].begin()&&(*it).score==(*(it-1)).score){    //若和前面一名的得分一样,名次也一样 
39                 (*it).localRank=(*(it-1)).localRank;  //vector是结构体类型时,*it要加括号
40             }else{
41                 (*it).localRank=k;
42             }
43             totalTestees.push_back(*it);    //将区域的考生添加进总体考生 
44             k++;
45         } 
46     }
47     sort(totalTestees.begin(),totalTestees.end(),cmp);
48     int m=1;
49     for(vector<Testee>::iterator it=totalTestees.begin();it!=totalTestees.end();it++){    //设置总名次 
50         if(it!=totalTestees.begin()&&(*it).score==(*(it-1)).score){    //若和前面一名的得分一样,名次也一样 
51             (*it).finalRank=(*(it-1)).finalRank;
52         }else{
53             (*it).finalRank=m;
54         }
55         m++;
56     }
57     cout<<m-1<<endl;
58     for(vector<Testee>::iterator it=totalTestees.begin();it!=totalTestees.end();it++){
59         cout<<(*it).id<<" "<<(*it).finalRank<<" "<<(*it).location<<" "<<(*it).localRank;
60         if(it!=totalTestees.end()){
61             cout<<endl;
62         } 
63     }
64     return 0;
65 } 

 

 

以上是关于PTA-1025——PAT Ranking的主要内容,如果未能解决你的问题,请参考以下文章

[PAT] A1025 PAT Ranking

[PAT] A1025 PAT Ranking

1025 PAT Ranking (25分)

PATA1025PAT Ranking

A 1141 PAT Ranking of Institutions (25分)

PAT甲级——A1025 PAT Ranking