admin 管理员组文章数量: 1184232
题目要求:
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.
这道题挺简单,注意一些细节就好了:
#include <iostream>
/**
* PAT Ranking
* @return
*/
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
struct Stu{
string number;
int loc;
int score;
int rank;
int locRank;
};
bool cmp(Stu* stu1,Stu* stu2){
if (stu1->score==stu2->score){
return stu1->number<stu2->number;
}
return stu1->score>stu2->score;
}
int main() {
int N;
cin>>N;
int K;
vector<Stu*> stus;
for (int i = 1; i <= N; ++i) {
cin>>K;
vector<Stu*> vector1;
for (int j = 0; j < K; ++j) {
Stu* stu1=new Stu();
cin>>stu1->number>>stu1->score;
stu1->loc=i;
stus.push_back(stu1);
vector1.push_back(stu1);
}
sort(vector1.begin(),vector1.end(),cmp);
int rank=1;
vector1[0]->locRank=rank;
for (int k = 1; k < vector1.size(); ++k) {
rank++;
if (vector1[k]->score==vector1[k-1]->score){
vector1[k]->locRank=vector1[k-1]->locRank;
} else{
vector1[k]->locRank=rank;
}
}
}
sort(stus.begin(),stus.end(),cmp);
int rank=1;
stus[0]->rank=rank;
for (int i = 1; i < stus.size(); ++i) {
rank++;
if (stus[i]->score==stus[i-1]->score){
stus[i]->rank=stus[i-1]->rank;
} else{
stus[i]->rank=rank;
}
}
cout<<stus.size()<<endl;
for (auto & stu : stus) {
cout<<stu->number<<" "<<stu->rank<<" "<<stu->loc<<" "<<stu->locRank<<endl;
}
return 0;
}版权声明:本文标题:解锁SWF与Flash中心的连结:PAT第二节点解析 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1770857039a3538354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论