根据网络上提供的程序进行优化,减少循环次数,效率提高30多倍。
#include <stdio.h>
#include <math.h>
int main(){
int year,month,day,n,day_back;
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("请输入年月日:\n");
while(scanf("%d-%d-%d,%d",&year,&month,&day,&n)!=EOF){
if((year%4==0&&year%100!=0)||year%400==0){//判断闰年
mon[2]=29;
}
else
mon[2]=28;
day_back=day;
day+=n;
for(int i=1;i<=n;i++)
{
if(day<=mon[month])
i=n+1;
else
{
if(i==1) i=mon[month]-day_back;
else i+=mon[month];
day=day-mon[month];
month++;
if(month>12){
year++;
month=1;
if((year%4==0&&year%100!=0)||year%400==0){//判断闰年
mon[2]=29;
}
else
mon[2]=28;
}
}
}
printf("%d %d %d\n",year,month,day);
}
return 0;
}