蓝桥集训之日期差值
-
模版:判断闰年 总天数 月份天数
-
#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int months[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int is_leap(int y){if(y % 100 && y % 4==0 || y%400 == 0)return 1; //是闰年就 +is_leap()return 0;}int get_months_days(int year,int month){int res = months[month];if(month==2) res += is_leap(year);return res;}int get_total_days(int y,int m,int d){int res =0;for(int i=1;i<y;i++)res += 365 + is_leap(i); //年总和for(int i=1;i<m;i++)res += get_months_days(y,i); //月总和return res + d;}int main(){int d1,d2,m1,m2,y1,y2;while(scanf("%04d%02d%02d", &y1,&m1,&d1) != -1){scanf("%04d%02d%02d", &y2,&m2,&d2);//从1年1月1日开始计算天数 作差+1cout<<abs(get_total_days(y1, m1, d1) - get_total_days(y2, m2, d2)) + 1<<endl;}}