forked from BenedictYoung/Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.6.cpp
49 lines (43 loc) · 1.15 KB
/
2.6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* 题目名称:日期差值
* 题目来源:上海交通大学复试上机题
* 题目链接:http://t.cn/E9Yz0LE
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int dayTable[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 IsLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int Number(int year, int month, int day) {
int number = 0;
for (int i = 1; i < year; ++i) {
if (IsLeapYear(i)) {
number += 366;
} else {
number += 365;
}
}
int row = IsLeapYear(year);
for (int j = 0; j < month; ++j){
number += dayTable[row][j];
}
number += day;
return number;
}
int main() {
int year, month, day;
while (scanf("%04d%02d%02d", &year, &month, &day) != EOF) {
int number1 = Number(year, month, day);
scanf("%4d%2d%2d", &year, &month, &day);
int number2 = Number(year, month, day);
printf("%d\n", abs(number1 - number2) + 1);
}
return 0;
}