1、题目
有两个日期,求两个日期之间的天数
输入:
20130101
20130105
输出:4
2、代码
#include<iostream>
#include<cstdio>
using namespace std;
int month[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 isyear(int x) {
return (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0);
}
int main() {
int x1, x2, ans = 0;
cin >> x1 >> x2;
if (x1 > x2) {
int temp = x1;
x1 = x2;
x2 = temp;
}
int y1 = x1 / 10000, m1 = (x1 % 10000) / 100, d1 = x1 % 100;
int y2 = x2 / 10000, m2 = (x2 % 10000) / 100, d2 = x2 % 100;
while (y1 < y2 || m1 < m2 || d1 < d2) {
d1++;
if (d1 == month[m1][isyear(y1)] + 1) {
d1 = 1;
m1++;
}
if (m1 == 13) {
m1 = 1;
y1++;
}
ans++;
}
cout << ans;
system("pause");
return 0;
}