PAT甲级真题-1012 The Best Rank详解优化

该博客介绍了一种C++解决方案,用于处理竞赛中学生的排名情况。通过定义结构体stu,存储每个学生的信息,包括ID、分数和名次,并使用sort函数进行排序。在处理相同分数时,代码能正确计算并输出每个学生的具体排名。此外,博客还展示了如何根据输入查询特定学生的最佳科目排名。

1012 The Best Rank

题目链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480

解题思路

1.显而易见,这题用结构体是很方便的;我们只需要在每一次比较结束之后对各个学生的名次进行排序;
2.对学生名次排序:第一名肯定是 1 ,但是第 i (i>1)名不一定是 i ,需要与i - 1名进行比较;

总结

sort排序可以写四个,也可以简写,用全局变量化简。

代码展示

#include<bits/stdc++.h>
using namespace std;
/*

*/
struct stu{
	int id;
	double score[5];
	int Rank[5];
};
stu student[2005];
int now;
bool cmp(stu a,stu b){
	return a.score[now]>b.score[now];
}
int rank1[1000000][5];
char sco[4]={'C','M','E','A'};
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>student[i].id;
		double sum=0;
		for(int j=0;j<3;j++){
			cin>>student[i].score[j];
			sum+=student[i].score[j];
		}
		student[i].score[3]=sum/3;
	}
	for(now =0;now<4;now++){
		sort(student,student+n,cmp);
		rank1[student[0].id][now]=1;
		for(int i=1;i<n;i++){
			if(student[i].score[now]==student[i-1].score[now])
				rank1[student[i].id][now]=rank1[student[i-1].id][now];
			else rank1[student[i].id][now]=i+1;
		}
	}
	while(m--){
		int id;
		cin>>id;
		if(!rank1[id][0])
			cout<<"N/A"<<endl;
		else {
			int k=3;
			for(int i=0;i<4;i++){
				if(rank1[id][i]<rank1[id][k])
					k=i;
			}
			cout<<rank1[id][k]<<" "<<sco[k]<<endl;
		}
	}
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高冷小伙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值