用Python
R7-1 计算某天距元旦的天数
分数 10
输入年、月、日,要求输出该年份的元旦到该日期总共经过了多少天。(提示:闰年是指能被400 整除的年份或者能被4 整除但不能100 整除的年份)
输入格式:
输入n,代表接下来要输入n行的年、月、日。
然后输入n行年、月、日,年、月、日之间的元素以空格相分隔,第1个数据为年,第2个数据为月,第3个数据为日。
输出格式:
如果输入年月日正确,则输出:Totaldays=天数
如果输入年月日有错,则输出:ErrorInput
输入样例:
4
2000 13 10
2000 3 10
2012 3 10
2018 5 20
输出样例:
在这里给出相应的输出。例如:
ErrorInput
Totaldays = 70
Totaldays = 70
Totaldays = 140
days = [31,28,31,30,31,30,31,31,30,31,30,31]
leap = lambda y: y%4==0 and y%100 or y%400==0
n = int(input())
res = []
for _ in range(n):
y,m,d = list(map(int,input().split()))
days[1] = 29 if leap(y) else 28
if y<0 or not 0<m<13 or not 0<d<=days[m-1]:
res.append('ErrorInput')
else:
res.append(f'Totaldays = {sum(days[:m-1])+d}')
for i in res:
print(i)
测试结果:
============================ RESTART: D:\test0.py ============================
4
2000 13 10
2000 3 10
2012 3 10
2018 5 20
ErrorInput
Totaldays = 70
Totaldays = 70
Totaldays = 140
给个例子参考:
def totaldays(ymd):
[y,m,d] = ymd
days = [31,28,31,30,31,30,31,31,30,31,30,31]
if y<0 or (m<0 or m>12):
return 'ErrorInput'
if (y%4==0 and y%100!=0) or y%400==0:
days[1] = 29
if d<0 or d>days[m-1]:
return 'ErrorInput'
else:
return sum(days[:m-1])+d
n = int(input())
lst = []
for i in range(n):
lst.append(input())
for i in range(n):
print(f'Totaldays = {totaldays(list(map(int,lst[i].split(" "))))}')