HDOJ 1201 18岁生日

本文详细解析了如何通过判断年份是否为闰年来计算特定月份的天数,涉及平年与闰年的区别及算法实现。

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


纯水题关键是得处理好闰年、闰月问题。

AC代码:

#include <iostream>
using namespace std;

int d1[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //平年
int d2[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //闰年

int y, d, m;

bool judge(int year){
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		return true;
	return false;
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--){
		int i = 0;
		int res = 0;
		scanf("%d-%d-%d", &y, &m, &d);

		if (d == 29){
			if (!judge(18 + y)){
				printf("-1\n");
				continue;
			}
		}

		for (int j = 0; j < m - 1; j++) //减去出生年的天数
		{
			if (judge(y))
				res -= d2[j];
			else
				res -= d1[j];
		}	
		res -= d;//出生日期减去
		
		while (i < 18){
			if (judge(y))
				res += 366;
			else
				res += 365;
			y++;
			i++;
		}
		for (int i = 0; i < m - 1; i++){
			if (judge(y))
				res += d2[i];
			else
				res += d1[i];
		}
		res += d;
		printf("%d\n", res);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值