1025 PAT Ranking

解题代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct student {
	char id[14];
	int score;
	int position;
	int prank;
	int rank;
}s[30000];
typedef struct student stu;
bool cmp1(stu a, stu b) {
	if (a.position != b.position) return a.position < b.position;
	return a.score > b.score;
}
bool cmp2(stu a, stu b) {
	if (a.score == b.score) return (strcmp(a.id, b.id) < 0);
	return a.score > b.score;
}
int main()
{
	int N, K, P = 1, index = 0;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		scanf("%d", &K);
		for (int j = 0; j < K; j++) {
			scanf("%s %d", s[index].id, &s[index].score);
			s[index].position = P;
			index++;
		}
		P++;
	}
	sort(s, s + index, cmp1);
	int r = 0;
	for (int i = 0; i < index; i++) {
		if (i && s[i].position != s[i - 1].position) s[i].prank = (r = 1);
		else if (i && s[i].score == s[i - 1].score) {
			s[i].prank = s[i - 1].prank;
			r++;
		}
		else s[i].prank = (++r);
	}
	sort(s, s + index, cmp2);
	for (int i = 0; i < index; i++) {
		if (i && s[i].score == s[i - 1].score) s[i].rank = s[i - 1].rank;
		else s[i].rank = i + 1;
	}
	printf("%d", index);
	for (int i = 0; i < index; i++)
		printf("\n%s %d %d %d", s[i].id, s[i].rank, s[i].position, s[i].prank);
	return 0;
}

测试结果

在这里插入图片描述

问题整理

1.排序算法。
2.参考中在最后的总排名输出中并未记录而是直接边处理编书处,在代码上会更精简。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值