题解 | #日期累加#

日期累加

http://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d

include<stdio.h>

include<stdbool.h>

int daytab[2][13] =
{
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};

bool isLeapYear(int year)
{
return (year%4 == 0&&year%100!=0)||(year%400 == 0);
}
int main()
{
int a,year,month,day,number,days;
scanf("%d",&a);
for(int i=0;i<a;i++)
{
while(scanf("%d\n%d%d%d",&year,&month,&day,&number)!=EOF)
{
days=day+number;
int row = isLeapYear(year);
while(days>daytab[row][month])
{
days-=daytab[row][month++];
if(month==13)
{
year++;
row=isLeapYear(year);
month=1;
}
}
printf("%04d-%02d-%02d\n",year,month,days);
}
}
return 0;
}

全部评论

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
点赞 评论 收藏
分享
2 收藏 评论
分享
牛客网
牛客企业服务