基础实验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;
}

猜你喜欢

转载自blog.csdn.net/yiwaite/article/details/103121240