一开始我想的是算出两个总日子相减,但太麻烦了,一定会超时
后来看算法笔记的方法是
用小的一直++到大的
#include<cstdio>
bool pd(int i)
{
return (i%400==0||(i%4==0&&i%100!=0));
}
int a[13][2]={{0,0},//这里二维数组的用法要掌握
{31,31},
{28,29},
{31,31},
{30,30},
{31,31},
{30,30},
{31,31},
{31,31},
{30,30},
{31,31},
{30,30},
{31,31}};
int main()
{
int time1,time2;
int yy1,yy2,mm1,mm2,dd1,dd2;
int i,j;
int s1,s2,s;
while(scanf("%d%d",&time1,&time2)!=EOF)
{
if(time1>time2)
{
int temp=time1;
time1=time2;
time2=temp;
}
yy1=time1/10000,
mm1=time1%10000/100,
dd1=time1%100;
yy2=time2/10000,
mm2=time2%10000/100,
dd2=time2%100;
s=1;
while(yy1<yy2||mm1<mm2||dd1<dd2)
{
dd1++;
if(dd1==a[mm1][pd(yy1)]+1)//超时在这,错了好几次,都没找到错误
{
mm1++;
dd1=1;
}
if(mm1==13)
{
yy1++;
mm1=1;
}
s++;
}
printf("%d\n",s);
}
return 0;
}
还有一次错的错误是输出超限,原因是因为一直死循环中
~scanf