PAT 1012

本文介绍了一个学生排名系统的设计与实现过程。系统通过输入学生的ID和各科成绩,自动计算总分并按科目进行排序,最后返回学生在不同科目中的最佳排名及对应的科目标识。文章详细展示了如何使用C++进行结构体定义、排序比较函数编写以及动态数组处理。

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

#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
using namespace std;

struct student
{
	int id;
	int score[4];
}st[1000010];

char map[4]={'A','C','M','E'};

int now;
int k[1000010][4]={0};//局部变量是放在栈里面的,栈的空间通常都不会太大,定义这么大一个局部数组,放在main函数内会导致栈溢出。
//一般来说,大数组最好不要定义为局部变量,要么改成全局变量,要么使用动态内存分配。 
bool cmp(student x,student y)
{
		return x.score[now]>y.score[now];
}

int main()
{	
	int n,m;
	
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d%d",&st[i].id,&st[i].score[1],&st[i].score[2],&st[i].score[3]);
		st[i].score[0]=st[i].score[1]+st[i].score[2]+st[i].score[3];//round
	}
	
	for(now=0;now<4;now++)
	{
		 sort(st,st+n,cmp);
		 k[st[0].id][now]=1;
		 for(int j=1;j<n;j++)
		 {
			 if(st[j].score[now]!=st[j-1].score[now])
				 k[st[j].id][now]=j+1;
			 else 
				 k[st[j].id][now]=k[st[j-1].id][now];
				
		 }
	}

	int query;
	for(int i=0;i<m;i++)
	{
		scanf("%d",&query);
		if(k[query][0]==0)
			printf("N/A\n");
		else
		{
			int x=0;
			for(int y=1;y<4;y++)
			{
				if(k[query][y]<k[query][x])
				{
					x=y;				
				}
			}
			printf("%d %c\n",k[query][x],map[x]);
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值