两日期间隔
给出两个时间输出他们间隔几天
如:
输入:
2013 1 1
2013 2 1
输出
31
代码如下:
#include<stdio.h>
int main()
{
int y[2],m[2],d[2],i,j,t,s,yca;
int lean(int year);
int cou(int nian,int yue,int ri);
scanf("%d %d %d",&y[0],&m[0],&d[0]);
scanf("%d %d %d",&y[1],&m[1],&d[1]);
if(y[0]>y[1])
{
t=y[1];y[1]=y[0];y[0]=t;
t=m[1];m[1]=m[0];m[0]=t;
t=d[1];d[1]=d[0];d[0]=t;
}
yca=y[1]-y[0];
if(yca==0)
{
i=cou(y[0],m[0],d[0]);
j=cou(y[1],m[1],d[1]);
if(i>j)
{t=j;j=i;i=t;}
printf("%d\n",j-i);
}
else if(yca==1)
{
if(lean(y[0])==1)
{
i=366-cou(y[0],m[0],d[0]);
j=cou(y[1],m[1],d[1]);
printf("%d\n",i+j);
}
else if(lean(y[0])==0)
{
i=365-cou(y[0],m[0],d[0]);
j=cou(y[1],m[1],d[1]);
printf("%d\n",i+j);
}
}
else if(yca>1)
{
int coun=0;
for(s=y[0]+1;s<y[1];s++)
{
if(lean(s)==1)
coun+=366;
else if(lean(s)==0)
coun+=365;
}
if(lean(y[0])==1)
{
i=366-cou(y[0],m[0],d[0]);
j=cou(y[1],m[1],d[1]);
printf("%d\n",i+j+coun);
}
else if(lean(y[0])==0)
{
i=365-cou(y[0],m[0],d[0]);
j=cou(y[1],m[1],d[1]);
printf("%d\n",i+j+coun);
}
}
return 0;
}
int lean(int year)
{
if(year%400==0||(year%4==0&&year%100!=0))
return 1;
else
return 0;
}
int cou(int nian,int yue,int ri)
{
int k,count=0;
for(k=1;k<yue;k++)
{
if(k==1||k==3||k==5||k==7||k==8||k==10||k==12)
count+=31;
else if(k==4||k==6||k==9||k==11)
count+=30;
else if(k==2)
{
if(lean(nian)==1)
count+=29;
else
count+=28;
}
}
count+=ri;
return count;
}