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++实现:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <vector>
 5 using namespace std;
 6 
 7 typedef struct Student
 8 {
 9     string id;        //考号
10     int score;        //分数
11     int location;    //地区
12     int local_rank;    //地区排名
13     int total_rank;        //总排名
14 }Student;
15 
16 bool myCmp(Student a, Student b)
17 {
18     if (a.score != b.score)
19     {
20         return a.score > b.score;
21     }
22     else
23     {
24         return a.id < b.id;
25     }
26 }
27 
28 int main()
29 {
30     int N;    //考场数
31     cin >> N;
32 
33     int localNum = 0;    //考场人数
34     int totalNum = 0;    //总人数
35     vector<Student> totalStus;    //保存所有考生的信息;
36     for (int i = 1; i <= N; ++i)
37     {
38         cin >> localNum;
39         vector<Student> localStus(localNum);
40         for (int j = 0; j < localNum; ++j)
41         {
42             cin >> localStus[j].id >> localStus[j].score;
43             localStus[j].location = i;    //i个考场
44         }
45         sort(localStus.begin(), localStus.end(), myCmp);
46         localStus[0].local_rank = 1;
47         totalStus.push_back(localStus[0]);
48         for (int j = 1; j < localNum; ++j)
49         {
50             if (localStus[j].score == localStus[j - 1].score)
51             {
52                 //分数相同
53                 localStus[j].local_rank = localStus[j - 1].local_rank;
54             }
55             else
56             {
57                 localStus[j].local_rank = j + 1;
58             }
59             totalStus.push_back(localStus[j]);
60         }    
61         totalNum += localNum;
62     }
63     
64     sort(totalStus.begin(), totalStus.end(), myCmp);
65     totalStus[0].total_rank = 1;
66     for (int i = 1; i < totalNum; ++i)
67     {
68         if (totalStus[i].score == totalStus[i-1].score)
69         {
70             totalStus[i].total_rank = totalStus[i - 1].total_rank;
71         }
72         else
73         {
74             totalStus[i].total_rank = i + 1;    //注意这里的细节
75         }
76     }
77     cout << totalNum << endl;
78     for (int i = 0; i < totalNum; ++i)
79     {
80         cout << totalStus[i].id << " " << totalStus[i].total_rank << " " << totalStus[i].location << " " << totalStus[i].local_rank << endl;
81     }
82     return 0;
83 }

注意:如果分数与前一名分数相同,那么他们的排名相同。如果分数小于前一名的分数,那他的排名就是循环次数+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; 按照分数从高到低排序

posted @ 2019-10-05 20:20  47的菠萝~  阅读(230)  评论(0编辑  收藏  举报