题解 | #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#include <iostream> using namespace std; int M[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; bool isrun(int y){ if(y%400==0||(y%4==0&&y%100!=0)) return true; return false; } void add(int& y,int& m,int& d){ if(isrun(y)) M[2]=29;else M[2]=28; d++;if(d>M[m]) {d=1;m++;if(m>12){m=1;y++;}} } int main() { string a, b;int ans=0; cin >> a >> b;if(a>b) swap(a,b); int y1=stoi(a.substr(0,4)); int m1=stoi(a.substr(4,2)); int d1=stoi(a.substr(6,2)); int y2=stoi(b.substr(0,4)); int m2=stoi(b.substr(4,2)); int d2=stoi(b.substr(6,2)); while(y1!=y2||m1!=m2||d1!=d2){ add(y1,m1,d1);ans++; } cout<<ans+1; } // 64 位输出请用 printf("%lld")