codeup墓地 日期累加

题目描述

设计一个程序能计算一个日期加上若干天后是什么日期。

输入

输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。

输出

输出m行,每行按yyyy-mm-dd的个数输出。

样例输入

1
2008 2 3 100

样例输出

2008-05-13

C++代码

#include<iostream>
#include<iomanip>
using namespace std;

//定义数组存储平年和闰年每月天数
int month[13][2] = {
	{0,0},
	{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},
	{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}
};
//判断是否是闰年
bool isLeap(int y) {
	return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int main() {
	int n;
	cin >> n;
	int y, m, d, a;
	int count;
	while (n--) {
		cin >> y >> m >> d >> a;
		count = 0;
		do{
			d++;
			if(d == month[m][isLeap(y)] + 1) {
				m++;
				d = 1;
			}
			if (m > 12) {
				m = 1;
				y++;
			}
			count++;
		} while (count < a);
		cout << setfill('0') << setw(4) << y;	//输出年
		cout << "-";
		cout << setfill('0') << setw(2) << m;	//输出月
		cout << "-";
		cout << setfill('0') << setw(2) << d;	//输出日
		cout << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值