某人三天打渔两天晒网,假设他从1990年1月1日开始打渔三天,然后晒网两天,请编程回答任意的一天他在打渔还是晒网。 A boy works for 3 days while has a 2 days off. If he is working on 1st, Jan, 1990, then for a date entered from the keyboard, please write a program to determine what the boy is doing, working or resting? Examples of input and output: 1)Input: 1990-01-05 Output: He is having a rest. 2)Input: 1990-01-07 Output: He is working. 3)Input: 1990-01-33 Output: Invalid input. ***输入数据格式***:"%4d-%2d-%2d" ***输出数据格式***:"Invalid input."或"He is having a rest." 或"He is working."
时间: 2024-02-04 21:01:51 浏览: 47
三天打渔两天晒网
可以使用datetime库中的datetime.date()函数来处理日期,先将输入的字符串转换为日期格式,然后计算与1990年1月1日相差的天数,再根据天数对5取模判断是打渔还是晒网。代码如下:
```python
import datetime
input_date = input()
try:
year, month, day = map(int, input_date.split('-'))
current_date = datetime.date(year, month, day)
start_date = datetime.date(1990, 1, 1)
days = (current_date - start_date).days
if days % 5 < 3:
print("He is working.")
else:
print("He is having a rest.")
except ValueError:
print("Invalid input.")
```
输入格式示例:1990-01-05,输出格式示例:He is having a rest.
阅读全文