PAT A1025 PAT Ranking
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. 参考代码: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 } 注意事项:? 无。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |