3498. 日期差值
摘要
Title: 3498. 日期差值
Tag: 日期
Memory Limit: 64 MB
Time Limit: 1000 ms
Powered by:NEFU AB-IN
3498. 日期差值
-
题意
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。
-
思路
- 用Python时调用datetime和timedelta库,直接进行计算即可,但也有限制,年数不能太大,会超限制
- 正常写法
-
代码
- 调库
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'''
Author: NEFU AB-IN
Date: 2023-05-18 17:20:27
FilePath: \Acwing\3498\3498.py
LastEditTime: 2023-05-18 18:31:46
'''
# import
import sys, math
from collections import Counter, deque
from heapq import heapify, heappop, heappush, nlargest, nsmallest
from bisect import bisect_left, bisect_right
from datetime import datetime, timedelta
from string import ascii_lowercase, ascii_uppercase
class sa:
def __init__(self, x, y):
self.x = x
self.y = y
def __lt__(self, x):
pass
# Final
N = int(1e3 + 10)
INF = int(2e9)
# Define
sys.setrecursionlimit(INF)
input = lambda: sys.stdin.readline().rstrip("\r\n") # Remove when Mutiple data
read = lambda: map(int, input().split())
letterTonumber = lambda x: ord(x.upper()) - 64
# —————————————————————Division line ——————————————————————
while True:
try:
s = input()
e = input()
if s > e:
s, e = e, s
d1 = datetime(year=int(s[:4]), month=int(s[4:6]), day=int(s[6:]))
d2 = datetime(year=int(e[:4]), month=int(e[4:6]), day=int(e[6:]))
print((d2 - d1).days + 1)
except:
break- 正常写法
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
using namespace std;
const int months[] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int is_leap(int year)
{
if (year % 100 && year % 4 == 0 || year % 400 == 0)
return 1;
return 0;
}
int get_month_days(int year, int month)
{
int res = months[month];
if (month == 2) res += is_leap(year);
return res;
}
int get_total_days(int y, int m, int d)
{
int res = 0;
for (int i = 1; i < y; i ++ )
res += 365 + is_leap(i);
for (int i = 1; i < m; i ++ )
res += get_month_days(y, i);
return res + d;
}
int main()
{
int y1, m1, d1, y2, m2, d2;
while (scanf("%04d%02d%02d", &y1, &m1, &d1) != -1)
{
scanf("%04d%02d%02d", &y2, &m2, &d2);
printf("%d\n", abs(get_total_days(y1, m1, d1) - get_total_days(y2, m2, d2)) + 1);
}
return 0;
}