题目描述
设计一个程序能计算一个日期加上若干天后是什么日期。
输入
输入第一行表示样例个数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;
}