日期累加
程序员文章站
2022-06-07 15:16:23
...
代码:
#include<stdio.h>
int y_year[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int n_year[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int n;
scanf("%d",&n);
int y,m,d;
int add;
while(n--)
{
scanf("%d %d %d %d",&y,&m,&d,&add);
d+=add;//天数增加
int *year;
if(y%100!=0&&y%4==0 || y%400==0)//判断是否为闰年
year=y_year;
else year=n_year;
while(d>year[m])
{
d=d-year[m];
m++;
while(m>12)//判断月份是否超过12
{
y++;
m-=12;
if(y%100!=0&&y%4==0 || y%400==0)//再次判断年份
year=y_year;
else year=n_year;
}
}
printf("%d-%02d-%02d\n",y,m,d);
}
return 0;
}