1025. PAT Ranking (25)

1. 原题: https://www.patest.cn/contests/pat-a-practise/1025

2. 思路:

题意:
对考生成绩进行局部和整体排名。
思路:
每个考生结构体进行排序就可以了。
排序函数可以单独写,为了简便,写在结构体里面(重载)。

3. 源码(已AC):

#include<iostream>
#include<vector>
#include<algorithm> //使用sort()
#include<string>
using namespace std;

struct stu	//学生的结构体
{
	bool operator<(const stu &b) const	//重载比较运算符
	{
		if(score != b.score)
			return score > b.score;
		else
			return id < b.id;
	}
	string id;	//存储id
	int score;
	int location;
	int local_rank;
	int total_rank;	//存储总排名
};

int main()
{
	//freopen("in.txt", "r", stdin);
	int N, K;
	vector<stu> person;
	int amount = 0;	//总人数
	cin >> N;
	vector<stu>::iterator it;//vector的迭代器
	for(int i = 1; i <= N; i++)
	{
		cin >> K;
		amount += K;
		for(int j = 0; j < K; j++)	//读入数据
		{
			stu student;
			cin >> student.id >> student.score;
			student.location = i;
			person.push_back(student);
		}

		it = person.end() - K;
		sort(it, person.end());	//排序
		int cur_rank = 1;
		int cnt = 0;
		int last_score = -1;

		for(it; it != person.end(); it++)	//局部排名
		{
			cnt++;
			if(it->score != last_score)
			{
				it->local_rank = cnt;
				cur_rank = cnt;
				last_score = it->score;
			}
			else
			{
				it->local_rank = cur_rank;
			}
		}
	}

	sort(person.begin(), person.end());
	int cur_rank = 1;
	int cnt = 0;
	int last_score = -1;

	for(it = person.begin(); it != person.end(); it++)//整体排名
	{
		cnt++;
		if(it->score != last_score)
		{
			it->total_rank = cnt;
			cur_rank = cnt;
			last_score = it->score;
		}
		else
		{
			it->total_rank = cur_rank;
		}
	}

	cout << amount << endl;	//输出结果
	for(it = person.begin(); it != person.end(); it++)
	{
		cout << it->id << " "
			<< it->total_rank << " "
			<< it->location << " "
			<< it->local_rank << endl;
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值