PTA 乙级 1028 人口普查

本文介绍了一段使用C语言编写的代码,用于判断生日的有效性并进行排序。代码中定义了一个生日结构体,包括姓名、年、月、日等字段,并实现了有效性判断和按年龄排序的功能。

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

有一点需要注意,如果有效生日数量是0,则输出0

代码实现:

#include<stdio.h>
#include<stdlib.h>

typedef struct
{
	char name[10]; int year, month, day;
}birth;

int Judge(birth item)
{
		if (item.year < 1814)return 0;
		else if (item.year == 1814 && item.month < 9)return 0;
		else if (item.year == 1814 && item.month == 9 && item.day < 6)return 0;
		else if (item.year > 2014)return 0;
		else if (item.year == 2014 && item.month > 9)return 0;
		else if (item.year == 2014 && item.month == 9 && item.day > 6)return 0;
		else return 1;
}

int main()
{
	int N;
	scanf("%d", &N);

	birth *a = (birth*)malloc(sizeof(birth)*N);
	int cnt = 0;
	for (int i = 0; i < N; i++)
	{
		birth item;
		scanf("%s %d/%d/%d", item.name, &item.year, &item.month, &item.day);
		if (Judge(item) == 1)a[cnt] = item, cnt++;
	}
	
	int oindex = 0, yindex = 0;
	for (int i = 0; i < cnt; i++)
		if (a[oindex].year > a[i].year)oindex = i;
		else if (a[oindex].year == a[i].year&&a[oindex].month > a[i].month)oindex = i;
		else if (a[oindex].year == a[i].year&&a[oindex].month == a[i].month&&a[oindex].day > a[i].day)oindex = i;
	for (int i = 0; i < cnt; i++)
		if (a[yindex].year < a[i].year)yindex = i;
		else if (a[yindex].year == a[i].year&&a[yindex].month < a[i].month)yindex = i;
		else if (a[yindex].year == a[i].year&&a[yindex].month == a[i].month&&a[yindex].day < a[i].day)yindex = i;
	if (cnt != 0)printf("%d %s %s", cnt, a[oindex].name, a[yindex].name);
	else printf("0");

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值