PAT(A)1025 PAT Ranking (25分)
程序员文章站
2022-07-08 22:20:48
...
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与成绩。
输出总人数,每个学生的id,总排名,考场排名,
按照总排名输出。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
struct node {
string id;
int final_rank, location_number, local_rank;
int score;
}a[30003];
bool cmp(node x, node y)
{
return x.score == y.score ? x.id < y.id : x.score > y.score;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
int num = 0;
cin >> n;
for (int i = 1; i <= n; ++i)
{
int k;
cin >> k;
for (int j = 1; j <= k; ++j)
{
cin >> a[num].id;
cin >> a[num].score;
a[num].location_number = i;
num++;
}
sort(a + num - k, a + num, cmp);
a[num - k].local_rank = 1;
for (int j = 1; j < k; ++j)
{
if (a[num - k + j].score == a[num - k + j - 1].score)
a[num - k + j].local_rank = a[num - k + j - 1].local_rank;
else
a[num - k + j].local_rank = j + 1;
}
}
sort(a, a + num, cmp);
a[0].final_rank = 1;
for (int i = 1; i < num; ++i)
{
if (a[i].score == a[i - 1].score)
a[i].final_rank = a[i - 1].final_rank;
else
a[i].final_rank = i + 1;
}
cout << num << endl;
for (int i = 0; i < num; ++i)
{
cout << a[i].id << " " << a[i].final_rank << " " << a[i].location_number << " " << a[i].local_rank << endl;
}
return 0;
}
上一篇: 算法导论学习笔记——7快速排序