首页 > 其他 > 详细

PAT A1025 PAT Ranking

时间:2019-08-28 19:51:48      阅读:73      评论:0      收藏:0      [点我收藏+]

PAT A1025 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 (≤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
  1234567890003 3 1 2
  1234567890004 5 1 4
  1234567890012 5 2 2
  1234567890002 7 1 5
  1234567890013 8 2 3
  1234567890011 9 2 4

参考代码:

 1 /**************************************************
 2 PAT A1025 PAT Ranking
 3 **************************************************/
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 
 9 using namespace std;
10 
11 struct Student {
12     string id;
13     int score;
14     int location;
15     int localRank;
16 
17     Student() : id(""), score(0), location(0), localRank(0) {}
18 };
19 
20 bool cmp(Student a, Student b) {
21     if (a.score != b.score) return a.score > b.score;
22     else return a.id < b.id;
23 }
24 
25 int main() {
26     int n = 0, k = 0, sum = 0;
27 
28     cin >> n;
29 
30     vector<Student> Location;
31     vector<Student> TotalLocations;
32 
33     for (int i = 0; i < n; ++i) {
34         cin >> k;
35 
36         Student temp;
37         for (int j = 0; j < k; ++j) {
38             cin >> temp.id >> temp.score;
39             temp.location = i + 1;
40             Location.push_back(temp);
41         }
42 
43         sort(Location.begin(), Location.end(), cmp);
44 
45         Location[0].localRank = 1;
46         for (int i = 1; i < k; ++i) {
47             if (Location[i].score == Location[i - 1].score)
48                 Location[i].localRank = Location[i - 1].localRank;
49             else
50                 Location[i].localRank = i + 1;
51         }
52 
53         TotalLocations.insert(TotalLocations.end(), Location.begin(), Location.end());
54         Location.clear();
55     }
56 
57     sort(TotalLocations.begin(), TotalLocations.end(), cmp);
58 
59     cout << TotalLocations.size() << endl;
60 
61     int totalRank = 1;
62     for (int i = 0; i < TotalLocations.size(); ++i) {
63         if (i > 0 && TotalLocations[i].score != TotalLocations[i - 1].score)
64             totalRank = i + 1;
65 
66         cout << TotalLocations[i].id <<   << totalRank <<  
67             << TotalLocations[i].location <<   << TotalLocations[i].localRank;
68 
69         if (i != TotalLocations.size() - 1)
70             cout << endl;
71     }
72 
73     return 0;
74 }

注意事项:

   无。

PAT A1025 PAT Ranking

原文:https://www.cnblogs.com/mrdragon/p/11425715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!