题目描述
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。
输入
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出
每组数据输出一行,即日期差值
样例输入
2013010120130105
样例输出
5
#include<cstdio>
//二维数组的行号可作为月份的标记
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}};
bool isLeap(int year){
return (year%4==0&&year%100!=0||year%400==0);
} //true可作为1,自动类型提升
int main(){
int time1,y1,m1,d1;
int time2,y2,m2,d2;
int ans = 1;
while(scanf("%d%d",&time1,&time2)!=EOF){
y1 = time1/10000;
y2 = time2/10000;
m1 = time1%10000/100;
m2 = time2%10000/100;
d1 = time1%100;
d2 = time2%100;
if(time1>time2){
int temp = time1;time1 =time2;time2=temp;
}
while(!(y1==y2&&m1==m2&&d1==d2)){ //time2不变,动time1
d1++;
if(d1>a[m1][isLeap(y1)]){
d1=1;m1++;
if(m1>12){
m1=1;
y1++;
}
}
ans++;
}
printf("%d",ans);
}
return 0;
}
codeup超时。。。。。