描述
设计一个程序能计算一个日期加上若干天后是什么日期。
输入描述:
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
输出描述:
输出m行,每行按yyyy-mm-dd的个数输出。
示例1
输入:
1
2008 2 3 100
输出:2008-05-13
要注意加上若干天后是否已经超过该年的总天数。
#include<iostream>
using namespace std;
int ping[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int run[]={0,31,60,91,121,152,182,213,244,274,305,335,366};
bool isrun(int year){
if((year % 4==0 && year % 100 != 0)||(year % 400==0)) return true;
else return false;
}
int main(){
int m;
cin>>m;
while(m--){
int year,month,day,add,total;
cin>>year>>month>>day>>add;
if(isrun(year)){ //确定年
total=run[month-1]+day+add;
while(total>366){
year++;
month=1;
total-=366;
}
}else{
total=ping[month-1]+day+add;
while(total>365){
year++;
month=1;
total-=365;
}
}
if(isrun(year)){ //确定月和日
for(int i=month;i<=12;i++){
if(total<=run[i]){
month=i;
total-=run[i-1];
printf("%04d-%02d-%02d\n",year,month,total);
break;
}
}
}else{
for(int i=month;i<=12;i++){
if(total<=ping[i]){
month=i;
total-=ping[i-1];
printf("%04d-%02d-%02d\n",year,month,total);
break;
}
}
}
}
return 0;
}