1025 PAT Ranking (25 分)

本文介绍了一个使用C++实现的竞赛排名系统,该系统能够处理多个考区的学生注册号、分数,并根据分数进行全局和分区排名。通过使用vector和sort函数,系统能够有效地管理和排序大量学生数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct student {
	string registration_number;
	int score, final_rank, local_rank, location_number;
};
bool cmp(student s1, student s2);
int main()
{
	int N;
	scanf("%d", &N);
	vector<student>stu, total_stu;
	student current_stu;
	int* KS = new int[N]; //N个记录,记录各个组的考试人数
	for (int i = 0; i < N; i++) {
		scanf("%d", &KS[i]);//输入第i+1个组的考试人数
		for (int j = 0; j < KS[i]; j++) {
			cin >> current_stu.registration_number >> current_stu.score;
			current_stu.location_number = i + 1;
			stu.push_back(current_stu);
		}
		sort(stu.begin(), stu.begin() + KS[i], cmp);
		int rank = 1;
		for (int k = 0; k < KS[i]; k++) {
			if (k > 0 && stu[k].score != stu[k - 1].score)rank=k+1;
			stu[k].local_rank = rank;
		}
		total_stu.insert(total_stu.end(), stu.begin(), stu.end()); //s将各个考区的记录插入总记录
		stu.clear();//清空第i+1个区的记录
	}
	sort(total_stu.begin(), total_stu.end(), cmp);
	int rank = 1;
		for (int k = 0; k < total_stu.size(); k++) {
			if (k > 0 && total_stu[k].score != total_stu[k - 1].score)rank=k+1;
			total_stu[k].final_rank = rank;
		}
    cout<<total_stu.size()<<endl;
	for (vector<student>::iterator it = total_stu.begin(); it != total_stu.end(); it++) {
		if((it+1)==total_stu.end())
			cout << (*it).registration_number << " " << (*it).final_rank << " " << (*it).location_number << " " << (*it).local_rank;
		else 
			cout << (*it).registration_number << " " << (*it).final_rank << " " << (*it).location_number << " " << (*it).local_rank << endl;
	}
	return 0;
}
bool cmp(student s1, student s2)
{
	if (s1.score != s2.score)return s1.score > s2.score;
	else return s1.registration_number < s2.registration_number;
}

运用sort vector的一些特性会非常方便

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值