PAT L2-027 名人堂与代金券(25 分)

本文介绍了一种比赛成绩处理及排名输出的方法,通过C++实现,重点在于如何正确使用字符串比较函数strcmp进行选手姓名的排序,同时确保分数相同的选手排名一致。

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

去年现场赛遇到的题目,当时能力还不够,没有做这道题

现在能做了,在最后并列输出的时候有点坑,并且千万注意字符串排序时候的strcmp函数返回的并不是0 1(好像是>0 <0这一类的),也不能直接楞比较两个字符大小,必须用strcmp

解决了这个问题以后谜一样的就从15变25了

题目链接
 

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct student
{
	char name[50];
	int score;
};
student s[10050];
int cmp(student a,student b)
{
	if(a.score==b.score)
	{
		if(strcmp(a.name,b.name)>0)
		{
			return 0;
		} //这里必须要使用strcmp 否则错 
		else
		{
			return 1;
		}
	}
	else
	{
		return a.score>b.score;
	}
}
int main(void)
{
	int n,g,k;
	scanf("%d%d%d",&n,&g,&k);
	int total = 0;
	for(int i=0;i<n;i++)
	{
		cin>>s[i].name>>s[i].score;
		if(s[i].score>=60&&s[i].score<g)
		{
			total += 20;
		}
		else if(s[i].score>=g&&s[i].score<=100)
		{
			total += 50;
		}
	}
	sort(s,s+n,cmp);
	printf("%d\n",total);
	int count = 0;
	int paiming[1005];
	for(int i=0;i<1005;i++)
	{
		paiming[i] = i+1;	
	}
	int temp;
	while(1)
	{
		printf("%d %s %d\n",paiming[count],s[count].name,s[count].score);
		temp = count;
		while(s[temp].score==s[temp+1].score)
		{
			paiming[temp+1] = paiming[temp];	
			temp++;
		}
		count++;
		if(count>=k&&s[count].score!=s[count-1].score)
		{
			break;
		}
	}
}
/*
10 80 5
cy@zju.edu.cn 78
cy@pat-edu.com 87
1001@qq.com 65
uh-oh@163.com 96
test@126.com 39
anyone@qq.com 87
zoe@mit.edu 80
jack@ucla.edu 88
bob@cmu.edu 80
ken@163.com 70
*/
/*
10 80 7
cy@zju.edu.cn 78
cy@pat-edu.com 87
1001@qq.com 78
uh-oh@163.com 96
test@126.com 39
anyone@qq.com 87
zoe@mit.edu 87
jack@ucla.edu 88
bob@cmu.edu 79
ken@163.com 70
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值