A1025 PAT Ranking [排序]

在这里插入图片描述
————————————————
注意点

  1. 学号(id)用字符串形式存储,如果用int型要用long long但是学号可能有前缀0,输出会省略前缀0,所以不行。
  2. 注意排名的实现!
#include<iostream>
#include<algorithm>
#include<cstring>
struct student
{
	char id[15];
	int score;
	int location;
	int local_rank;
	int all_rank;
}stu[30001];
using namespace std;
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 n, k, num = 0;
	int count = 0;
	cin >> n;
	for (int i = 1; i <=n; i++)
	{
		cin >> k;
		count += k;
		for (int j = 0; j < k; j++)
		{
			cin >> stu[num].id >> stu[num].score;
			stu[num].location = i;
			num++;
		}
		sort(stu + num - k, stu + num, cmp);
		stu[num - k].local_rank = 1;
		for (int j = num - k + 1; j < num; j++)
		{
			if (stu[j - 1].score == stu[j].score)
			{
				stu[j].local_rank = stu[j-1].local_rank;
			}
			else
			{
				stu[j].local_rank = j + 1 - (num - k);
			}
		}
	}
	sort(stu, stu + num, cmp);
	stu[0].all_rank = 1;
	for (int j = 1; j < num; j++)
	{
		if (stu[j - 1].score == stu[j].score)
		{
			stu[j].all_rank = stu[j-1].all_rank;
		}
		else
		{
			stu[j].all_rank = j + 1;
		}
	}
	cout << count << endl;
	for (int i = 0; i < num; i++)
	{
		cout << stu[i].id << " " << stu[i].all_rank << " " <<stu[i].location<<" "<< stu[i].local_rank << endl;
	}
}
原文地址:https://www.cnblogs.com/Hsiung123/p/13812085.html