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.html
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.ios
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:nginx
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.git
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
有N個(<= 100)考場,每一個考場有K(<= 300)名考生,給出每一個考場的考生的考號,分數數組
輸出全部考生的編號,總排名,考場號,考場內排名(按照非遞減順序輸出)函數
若是考生的分數相同,就按照編號小到大輸出(考號小的優先輸出)flex
先按照考場內對考生進行排序,將結果保存到總考生數組,再對總考生進行排序spa
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)code
1. sort函數第三個參數cmp能夠本身編寫排序規則。orm
bool myCmp(Student a, Student b) { return a.score > b.score; }
自定義排序規則:按照Student中的score來排序, return a.score > b.score; 按照分數從高到低排序