PAT 28/95 乙级 人口普查

#include
#include
#include
struct stu
{
	char name[11];
	int year;
	int month;
	int day;
}stu1[100000], stu2[100000];
int cmp1(const void *a, const void *b)
{
	if ((*(struct stu *)a).year !=(*(struct stu*)b).year)
	{
		return (*(struct stu *)a).year <(*(struct stu*)b).year;
	}
	else if ((*(struct stu *)a).month != (*(struct stu*)b).month)
	{
		return (*(struct stu *)a).year < (*(struct stu*)b).year;
	}
	else if ((*(struct stu *)a).day != (*(struct stu*)b).day)
	{
		return (*(struct stu *)a).year < (*(struct stu*)b).year;
	}

}
int cmp(const void *a, const void *b)
{
	if ((*(struct stu *)a).year >(*(struct stu*)b).year)
	{
		return 1;
	}
	else if ((*(struct stu *)a).year ==(*(struct stu*)b).year)   //qsort 三种情况都得写  卡在这里好久测试点014
	{
		if ((*(struct stu *)a).month > (*(struct stu*)b).month)
		{
			return 1;
		}
		else if ((*(struct stu *)a).month ==(*(struct stu*)b).month)
		{
			if ((*(struct stu *)a).day > (*(struct stu*)b).day)
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}
		else
			return -1;
	}
	else
		return -1;
}
int main()
{
	int n;
	int num = 0;
	scanf("%d",&n);
	for (int i = 0; i1814 && stu1[i].year<2014 || (stu1[i].year == 1814 && stu1[i].month>9) || (stu1[i].year == 1814 && stu1[i].month == 9 && stu1[i].day >= 6) || (stu1[i].year == 2014 && stu1[i].month<9) || (stu1[i].year == 2014 && stu1[i].month == 9 && stu1[i].day <= 6)) {
			stu2[num] = stu1[i];
			num++;
		}
	}
	qsort(stu2, num, sizeof(struct stu), cmp);
	if (num == 0) printf("0");
	else
	{
		printf("%d %s %s", num, stu2[0].name, stu2[num - 1].name);
	}
	//	while (1);
	return 0;
}

思路没问题。但是测试点014一直过不去网上也查了一下。试着找了一下原因始终没解决。最后是qsort排序三种情况都要写 。最后ac

你可能感兴趣的:(PAT乙级,C语言)