我正在尝试将(day,hour,minut,seconds)
转换为2个日期的秒数,然后改变,然后反转。我所做的基本上有效但在某些情况下会返回错误的结果。
要测试的输入数据表单:
13
6 0 8 22 20 18 10 28
2 5 32 20 7 1 21 51
11 5 54 35 15 15 11 24
6 2 3 49 16 10 49 9
1 1 37 59 14 22 13 0
3 15 15 10 28 18 37 21
21 9 54 11 22 1 11 13
12 3 37 24 20 12 42 50
16 11 25 12 26 1 37 19
7 4 59 31 12 8 54 23
3 10 22 49 24 19 36 32
1 1 21 2 29 10 51 13
5 22 2 37 29 1 53 59
它总是在小时结果中返回一个错误的值(它增加了36)
代码:
count=lambda a,b,c,d:a+b+c+d
def magic_date_to_numbers(a,b,c,d):
a*=60**3
b*=60**2
c*=60
return count(a,b,c,d)
def reverse_magic(n):
a=n/(60**3)
b=(n%(60**3))/(60**2)
c=(((n%60**3)%60**2))/60
d=((n%(60**3))%(60**2))%60
print "(%s %s %s %s) "%(str(a),str(b),str(c),str(d))
for i in range(0,int(raw_input())):
d=map(long,raw_input().split(" "))
n1=magic_date_to_numbers(d[0],d[1],d[2],d[3])
n2=magic_date_to_numbers(d[4],d[5],d[6],d[7])
reverse_magic(abs(n2-n1))
谢谢:)
答案 0 :(得分:0)
已经有非常好的内置插件和实用程序来处理这些类型的问题,但我认为你所犯的错误很简单:一天24小时,而不是60(60 - 24 = 36)。
from datetime import timedelta
def magic_date_to_numbers(days, hours, minutes, seconds):
t = timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
return t.total_seconds()
# hours += days * 24 # 24 hours in a day
# minutes += hours * 60 # 60 minutes in an hour
# seconds += minutes * 60 # 60 seconds in a minute
# return seconds
def reverse_magic(n):
t = timedelta(seconds=n)
n = t.seconds
seconds, minutes = n % 60, n // 60
minutes, hours = minutes % 60, minutes // 60
# hours, days = hours % 24, hours // 24
print "(%s %s %s %s) " % (t.days, hours, minutes, seconds)
for i in range(0,int(raw_input())):
d=map(long,raw_input().split(" "))
n1=magic_date_to_numbers(d[0],d[1],d[2],d[3])
n2=magic_date_to_numbers(d[4],d[5],d[6],d[7])
reverse_magic(abs(n2-n1))