题解 | #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#include <iostream> using namespace std; int GetYearDay(int a,int b,int c) { int sum=0; //如果是闰年,并且此时已经超过二月 if(((a%4==0&&a%100!=0)||a%400==0)&&b>2) ++sum; //当月不用加上本月的最大值,为方便后续判断先减去一 b--; while(b>0) { if(b==1||b==3||b==5||b==7||b==8||b==10||b==12) sum+=31; else if(b==2) sum+=28; else sum+=30; b--; } sum+=c; return sum; } int main() { int a, b; while (cin >> a >> b) { //默认a>b. if(a<b) { int tmp=a; a=b; b=tmp; } int year1=a/10000; int month1=(a%10000)/100; int day1=a%100; int year2=b/10000; int month2=(b%10000)/100; int day2=b%100; int sum=1; sum+=365*(year1-year2); //CV下某一题的代码(日期到天数的转换) sum+=GetYearDay(year1,month1,day1)-GetYearDay(year2,month2,day2); cout<<sum<<endl; } return 0; }