您的位置:首页 > 其它

PAT (Advanced) 1025. PAT Ranking (25)

2018-01-01 21:36 465 查看
原题:1025. PAT Ranking (25)

解题思路:简单的模拟,对每一组数据进行排序,记录考场排名,最后对所有数据进行排序,记录最终排名

c++代码如下:

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct Student{
char id[15];
int score;
int final_rank;
int lc_rank;
int lc_num;
};
vector<Student> stu;
bool cmp(Student a, Student b)
{
if(a.score != b.score)
return a.score > b.score;
else
return strcmp(a.id, b.id) < 0;
}

int main()
{
int k;
while(scanf("%d", &k) != EOF)
{
if(k == 0)
printf("0\n");
else
{
int cnt = 0;//记录考生总数
for(int i = 0; i < k; i++)
{
int n;
int r = 1;
Student x;
scanf("%d", &n);
//对每一小组的处理
for(int j = 0; j < n; j++)
{
scanf("%s", x.id);
scanf("%d", &x.score);
x.lc_num = i + 1;
stu.push_back(x);
}
sort(stu.begin() + cnt, stu.begin() + cnt + n, cmp);
stu[cnt].lc_rank = 1;
for(int j = cnt + 1; j < cnt + n; j++)
{
if(stu[j].score == stu[j-1].score) //分数相同时 排名相同
stu[j].lc_rank = stu[j-1].lc_rank;
else
stu[j].lc_rank = j - cnt + 1;
}

cnt += n; //加上本组考生
}

//对总体排名进行计算
sort(stu.begin(), stu.end(), cmp);
stu[0].final_rank = 1;
for(int i = 1; i < cnt; i++)
{
if(stu[i].score == stu[i-1].score)
stu[i].final_rank = stu[i-1].final_rank;
else
stu[i].final_rank = i + 1;
}
//输出
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
{
printf("%s %d %d %d\n", stu[i].id, stu[i].final_rank, stu[i].lc_num, stu[i].lc_rank);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: