题目描述
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。
输入
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出
每组数据输出一行,即日期差值
样例输入
20130101
20130105
样例输出
5
#include <stdio.h>
#include <string.h>
int num[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 isPrime(int x){
if ((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0)){
return 1;
}else return 0;
}
int main()
{
int num1,num2,temp;
int year1,year2,month1,month2,day1,day2;
while(scanf("%d%d",&num1,&num2) != EOF){
if(num1 > num2){
temp = num1;
num1 = num2;
num2 = temp;
}
year1 = num1 / 10000,month1 = num1 % 10000 / 100,day1 = num1 % 100;
year2 = num2 / 10000,month2 = num2 % 10000 / 100,day2 = num2 % 100;
int cnt = 1;
while(year1 < year2 || month1 < month2 || day1 < day2){
day1++;
if(day1 == num[month1][isPrime(year1)] + 1){
month1++;
day1 = 1;
}
if(month1 == 13){
year1++;
month1 = 1;
}
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}