目录
题目
题目链接:1025 PAT Rankinghttps://pintia.cn/problem-sets/994805342720868352/problems/994805474338127872
测试样例
输入样例
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
提交结果截图
解题思路及源代码
/*********************************************************************
*解题思路:
1.输入:每个输入文件包含一个测试用例。
对于每种情况,第一行包含一个正数N(≤100),即测试位置的数量。
然后是N个排名表,每个排名表的开头一行是正整数K(≤300),
代表被试人数,然后是K行,代表注册号(13位数字)和被试总分。
一行中的所有数字用一个空格隔开。
2.输出:对于每个测试用例,首先在一行中打印被测试者的总数。
然后按以下格式打印最终排名:
Registration_number final_rank location_number local_rank
这些位置编号从1到n。输出必须按最终排名的非递减顺序排序。
得分相同的被试必须有相同的排名,并且输出必须按其注册号的非递减顺序排序。
3.根据输入输出可知难点为:
a.对测试者的分数进行本地排名(即该测试者分数在其测试位置的排名)。
b.对测试者的分数进行全部排名(即总排名)
c.对测试者的id进行排名(总排名相同时,id小的排在前)
4.解法:
用结构体存储测试者的信息(id,测试位置,测试分数,本地排名,全体排名,id排名)
分别得出各项排名。
*
**********************************************************************/
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct Testee
{
char id[14];//id
int score;//测试分数
int location_number;//测试位置
int local_rank;//本地排名
int final_rank;//全体排名
}testees[30005];//最多可能有300*100个测试者
bool cmp(Testee a, Testee b)
{
if (a.score != b.score)
return a.score > b.score;//分数从高到低排序
else
return strcmp(a.id, b.id) < 0;//id从小到大排序
}
int main()
{
int n, k, num = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> k;
int start_loc = num;//开始排序位置
for (int j = 0; j < k; j++)
{
cin >> testees[num].id >> testees[num].score;
testees[num++].location_number = i;
}
sort(testees + start_loc, testees + num, cmp);//对一个测试位置中的测试者分数进行排序
testees[start_loc].local_rank = 1;//将第一个学生排名先定为第1,后面就依次类推
for (int j = start_loc + 1; j < num; j++)
if (testees[j].score < testees[j - 1].score) //若是比前一个人的分数低,排名为当前位置
testees[j].local_rank = j - start_loc + 1;
else //若是分数相等,排名与前一人相等
testees[j].local_rank = testees[j - 1].local_rank;
}
sort(testees, testees + num, cmp);//对全体测试者排序
testees[0].final_rank = 1;//与本地排名的方式一致,可以写个函数避免代码重复
for (int i = 1; i < num; i++)
if (testees[i].score < testees[i - 1].score)
testees[i].final_rank = i + 1;
else
testees[i].final_rank = testees[i - 1].final_rank;
cout << num << endl;
for (int i = 0; i < num; i++)
cout << testees[i].id << " " << testees[i].final_rank << " " << testees[i].location_number << " " << testees[i].local_rank << endl;
return 0;
}