学习笔记,仅供参考。
#include<iostream>
using namespace std;
int months[13][2] = {{0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}};
bool isleap(int y){
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
void calculate(int year1, int year2, int m1, int m2, int d1, int d2, int sum){
while(year2 < year1 || m2 < m1 || d2 < d1){
d2++;
if(d2 == months[m2][isleap(year2)] + 1){
d2 = 1;
m2++;
}
if(m2 == 13){
m2 = 1;
year2++;
}
sum++;
}
cout << sum << endl;
}
int main(){
int time1, year1, m1, d1;
int time2, year2, m2, d2;
cin>>time1>>time2;
int temp = 0;
if(time1 < time2){
temp = time1;
time1 = time2;
time2 = temp;
}
year1 = time1 / 10000;
year2 = time2 / 10000;
m1 = time1 % 10000 / 100;
m2 = time2 % 10000 / 100;
d1 = time1 % 100;
d2 = time2 % 100;
int ans = 1;
if(year2 < year1 - 1){
if(isleap(year2)){
ans += 366;
}
else{
ans += 365;
}
year2++;
}
calculate(year1,year2,m1,m2,d1,d2,ans);
return 0;
}