1025 PAT Ranking (25分)
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
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
小结
这个题还是比较清晰的,没有什么太大的难度。常规题型,以后再涉及到固定长度的id就用string储存,不然会失去前导0,string也可以比大小,从头到尾,一个字符一个字符的比较。
错误
没有考虑到ID的前导0情况,导致输出的ID有可能长度不够
#include <iostream>
#include<string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student
{
string ID;
int score;
int rank;
int loc;
int locrank;
};
int N;
vector <Student> group[100];
//总排名,组号,本组排名 算并列
bool Gsort(Student s1, Student s2) {
if (s1.score == s2.score) {
return s1.ID < s2.ID;
}else return s1.score > s2.score;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++)
{
int M;
cin >> M;
for (int j = 0; j < M; j++)
{
Student student;
cin >> student.ID >> student.score;
student.loc = i;
group[i].push_back(student);
}
sort(group[i].begin(), group[i].end(), Gsort);
int nowRank=1;
for (int k = 0; k < group[i].size(); k++)
{
if (k == 0) {
group[i][k].locrank = 1;
continue;
}
if (group[i][k-1].score == group[i][k].score) {
group[i][k].locrank = nowRank;
}
else {
nowRank = k + 1;
group[i][k].locrank = nowRank;
}
}
}
vector<Student>sumStudent;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < group[i].size(); j++)
{
sumStudent.push_back(group[i][j]);
}
}
sort(sumStudent.begin(), sumStudent.end(),Gsort);
int nowRank = 1;
for (int k = 0; k < sumStudent.size(); k++)
{
if (k == 0) {
sumStudent[k].rank = 1;
continue;
}
if (sumStudent[k - 1].score == sumStudent[k].score) {
sumStudent[k].rank = nowRank;
}
else {
nowRank = k + 1;
sumStudent[k].rank = nowRank;
}
}
cout << sumStudent.size() << "\n";
int sz = sumStudent.size();
for (int i = 0; i < sz; i++)
{
cout << sumStudent[i].ID << " " << sumStudent[i].rank << " ";
cout << sumStudent[i].loc + 1 << " " << sumStudent[i].locrank << "\n";
}
return 0;
}