1025 PAT Ranking

题目链接
大意:对多个考场的多个学生排名不仅有总排名还要有分区排名;
代码中有注释;

#pragma warning(disable:4996);
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<math.h>
#include<vector>
using namespace std;
struct stu {
	char id[15];
	int score;
	int localnumber;//考场号;
	int localrank;//所在考场内的排名;
	bool operator<(const stu& a) const
	{
     if (a.score !=score) return a.score <score;
	else  return strcmp(id, a.id) < 0;
	}
}buf[30010];
//现按分数由大到小,若相同则利用准考证号由小到大决出排名;
int main() {
	int n, k, num=0;
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> k;//考场人数;
		for (int j = 0;j < k;j++) {
			getchar();
			cin >> buf[num].id >> buf[num].score;
			buf[num].localnumber = i;
			num++;
		}
		sort(buf + num - k, buf + num);//利用k和num同一考场内的进行排序
		//这个考场考生所对应的下标为[num-k,num);
		buf[num - k].localrank = 1;
		//对考场其余人排名;
		for (int j = num - k + 1;j < num;j++) {
			if (buf[j].score == buf[j - 1].score)buf[j].localrank = buf[j - 1].localrank;
			else {//如果与前一个学生不同分
				buf[j].localrank = j  - (num - k)+ 1;
			}
		}
	}
	printf("%d\n", num);//总人数
	sort(buf, buf + num);//总排名;
	int r = 1;
	for (int i = 0;i < num;i++) {
		if (i > 0 && buf[i].score != buf[i - 1].score) {
			r = i + 1;//当与上一个人分数不同时,让r更新为人数加1;
		}
		printf("%s ", buf[i].id);
		printf("%d %d %d\n", r, buf[i].localnumber, buf[i].localrank);
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值