//zzuli 1878
1.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
#define N 1010000
#define INF 0x3f3f3f3f
int yearday[N];
int IsLeap(int year)
{
return ((year%4==0&&year%100!=0) || year%400==0);
}
int main()
{
int T, y1, m1, d1, y2, m2, d2, sum;
int tab[2][13]={{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int leap;
yearday[1899]=0;
for(int i=1900; i<=1000000; i++)
yearday[i]=yearday[i-1]+(IsLeap(i)==1 ? 366 : 365);
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2);
sum=0;
if(y1==y2)
{
leap=IsLeap(y1);
for(int i=m1+1; i<m2; i++)
sum+=tab[leap][i];
if(m1<m2)
{
sum+=tab[leap][m1]-d1;
sum+=d2;
}
else
sum+=d2-d1;
printf("%d\n", sum);
}
else
{
leap=IsLeap(y1);
for(int i=m1+1; i<=12; i++)
sum+=tab[leap][i];
sum+=tab[leap][m1]-d1;
leap=IsLeap(y2);
for(int i=1; i<m2; i++)
sum+=tab[leap][i];
sum+=d2;
sum+=yearday[y2-1]-yearday[y1];
printf("%d\n", sum);
}
}
return 0;
}
2.另解:我直接采用了1年1月1日到两个输入的日期分别有多少天然后计算差值。这样的话关于闰年比较好计算,可以用容斥...闰年数量为x/4-x/100+x/400...
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
#define N 100100
#define INF 0x3f3f3f3f
int tab[2][13]={{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int IsLeap(int y)
{
return ((y%4==0&&y%100!=0) || (y%400==0));
}
int js(int y, int m, int d)
{
int sum=0, t, leap;
t=(y-1)/4-(y-1)/100+(y-1)/400;
sum+=365*(y-1)+t;
leap=IsLeap(y);
for(int i=1; i<m; i++)
sum+=tab[leap][i];
sum+=d;
return sum;
}
int main()
{
int T, y1, m1, d1, y2, m2, d2, ans1, ans2;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2);
ans1=js(y1, m1, d1);
ans2=js(y2, m2, d2);
printf("%d\n", ans2-ans1);
}
return 0;
}