标签:
指定日期计算出N天后的日期问题
问题描述:先输入起始日期,再输入任意天数n,求n天后的日期。
源代码一(参考http://zhidao.baidu.com/linkurl=0BxF0QvTywzuRkLUFj6ulUQKYqp97CjUOZBV1UUmXoCkS9HNyOT2axHkcOsRLi-xlB_JE6KvdglSD23S35EUXK)
#include<stdio.h><span style="font-family:SimHei;font-size:14px;"><strong>int main() { int year,month,day; int leapyear=0; int daytime=0; int sum; static int month_date[12]={31,28,31,30,31,30,31,31,30,31,30,31}; printf("请输入日期:"); scanf("%d-%d-%d",&year,&month,&day); printf("请输入天数:"); scanf("%d",&daytime); sum=daytime+day; do { if(month==2) { month_date[month-1]+=leapyear; } if(sum>month_date[month-1]) { sum-=month_date[month-1]; month++; if(month==13) { year++; if(year%400==0||(year%100!=0&&year%4==0)) { leapyear=1; } else { leapyear=0; } month=1; } } } while(sum>month_date[month-1]); day=sum; printf("\n第%d天后是%d-%d-%d",daytime,year,month,day); return 0; }</strong></span><span style="font-family: arial, 'courier new', courier, 宋体, monospace; font-size: 14px;"> </span><span style="font-family:SimHei;font-size:14px;"><strong> </strong></span>源代码二(自己写的)
#include<stdio.h>
int year,month,day,days,years;
int a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int ru(int year)
{
if(year%400==0||year%4==0&&year%100!=0)
return 1;
else
return 0;
}
void rest(int days)
{
int i;
for(i=1;i<=12;i++)
{
if(days-a[i]>0)
days=days-a[i];
else
{
month=i;
day=days;
break;
}
}
if(i-1==12)
{
year=year+1;
if(!ru(year))
days++;
rest(days);
}
year+=years;
}
int main()
{
int i;
while(scanf("%d%d%d%d",&year,&month,&day,&days)==4)
{
days=days-(a[month]-day);
if(!ru(year)&&month<=2)
days=days+1;
for(i=month+1;i<=12;i++)
{
if(days>a[i])
days=days-a[i];
else
break;
}
if(i-1==12)
{
printf("%d\n",days);
year++;
years=0;
while(days>=366)
{
days=days-366;
years++;
}
for(i=1;i<=years;i++)
{
if(!ru(year+i))
days++;
}
rest(days);
}
else
{
month=i;
day=days;
}
printf("%d-%d-%d\n",year,month,day);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u013514928/article/details/46052027