基础实验7-2.4 PAT排名汇总 (25 分)(慎用long long类型)

这道练习题一开始把学生结构体中的学生对考号对数据类型设置为long long类型,我这么设置是因为考号是13位的整型,结果写完程序后发现最后一个测试点始终是答案错误。后来我把考号的数据类型设置为了string类型才过了所有测试点。我很迷糊这一点,有没有知道我这错误的同学能告诉我一下,感激不尽!我先把这个错误先记录下来,在以后的学习中看能不能知道为什么。下面是代码和相关说明:

#include<cstdio>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 30005;
struct student
{
	string id;//如果是long long就过不了最后一个测试点
	int score;
	int finalRank;
	int area;
	int areaRank;
};
bool cmp(student a, student b)
{
	if(a.score!=b.score)
	{
		return a.score>b.score;
	}
	else
	{
		return a.id<b.id;
	}
}
struct student stu[maxn];
int main()
{
	int N,i,j,k,z,K,tmpRank,lastScore;
	scanf("%d",&N);
	int index = 0;
	for(i=1; i<=N; i++)
	{
		scanf("%d",&K);
		struct student S[K];
		for(j=0; j<K; j++)
		{
            cin>>S[j].id;
			scanf("%d",&S[j].score);
		}
		sort(S,S+K,cmp);
		lastScore = -1;tmpRank = -1;
		for(k=0; k<K; k++)
		{
			if(S[k].score==lastScore)
			{
				S[k].areaRank = tmpRank;
			}
			else
			{
				S[k].areaRank = k+1;
				tmpRank = k+1;
				lastScore = S[k].score;
			}
		}
		for(z=0; z<K; z++)
		{
			stu[index].id = S[z].id;
			stu[index].areaRank = S[z].areaRank;
			stu[index].area = i;
			stu[index].score = S[z].score;
			index++;
		}
	}
	sort(stu,stu+index,cmp);
	lastScore = -1; tmpRank = -1;
	for(z=0; z<index; z++)
	{
		if(stu[z].score == lastScore)
		{
			stu[z].finalRank = tmpRank;
		}
		else
		{
			stu[z].finalRank = z+1;
			tmpRank = z+1;
			lastScore = stu[z].score;
		}
	}
	printf("%d\n",index);
	for(z=0; z<index; z++)
	{
        cout<<stu[z].id;
		printf(" %d %d %d\n",stu[z].finalRank,stu[z].area,stu[z].areaRank);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值