题解 | #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#include <iostream> using namespace std; int arr[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} }; bool isleap(int year){ if(year%400==0||year%100!=0&&year%4==0) //判断是否为闰年 { return true; } return false; } int main() { int year1,month1,day1,date1; int year2,month2,day2,date2; while(scanf("%d",&date1)!=EOF){ scanf("%d",&date2); year1 = date1 /10000; month1 = (date1 %10000)/100; day1 = date1%100; year2 = date2 /10000; month2 = (date2 %10000)/100; day2 = date2%100; int gap = 1; while(year1*10000+month1*100+day1 < year2*10000+month2*100+day2) //实现d1不断加一直到与d2相等 { gap++; day1++; if(arr[isleap(year1)][month1]==day1){ month1++; day1=0; if(month1==13){ year1++; month1=1; } } } printf("%d",gap); } } // 64 位输出请用 printf("%lld")