Blue Bridge Cup Daily One Question (1): Gauss Diary (python)

Topic:

The great mathematician Gauss has a good habit: keep a diary anyway.
There is a difference in his diary. He never noted the year, month, and day, but instead used an integer instead. For example, after 4210,
people knew that the integer was the date, and it said that day was the day after Gauss was born. day. This may also be a good habit. It reminds the owner all the time: One day has passed, how much time is left to waste?
Gauss was born: April 30, 1777.
In the diary of an important theorem discovered by Gauss, it is marked: 5343, so it can be calculated that the day is: December 15, 1791.
The diary on the day when Gauss received his doctorate is marked: 8113
, please calculate the date on which Gauss received his doctorate

Solution:

(It can be seen from the example that the birth is also a day)
As it is related to the date, it is easy to think of the datetime module.
First use the strptime function to convert the date of birth into a datetime format for subsequent processing.
After that, use the deltatime function to convert days to delta days (incremental time) )
Finally, add the date of birth and the incremental time to get the final time.
Use the strftime method to finally output the time.
It can also be directly output, but the direct output result includes minutes and seconds

Code_1:

import datetime
days = int(input('日记时间:')) - 1

birth = datetime.datetime.strptime('1777-4-30', '%Y-%m-%d')

delta = datetime.timedelta(days=days)

doctor = birth + delta

print(doctor.strftime('%Y-%m-%d'))

Code_2:

General method, not limited to this question

import datetime
days = int(input('间隔天数:')) - 1
year = str(input('开始年份:'))
month = str(input('开始月份:'))
day = str(input('开始日期:'))

start = [year, month, day]
start = '-'.join(start)

birth = datetime.datetime.strptime(start, '%Y-%m-%d')

delta = datetime.timedelta(days=days)

doctor = birth + delta

print(doctor.strftime('%Y-%m-%d'))

Guess you like

Origin blog.csdn.net/weixin_50791900/article/details/112276853