问题如下:
迪克是d = 12岁。当我们这样说时,我们的意思是,自迪克出生以来,这至少是十二年,而不是十三年。 迪克和简有三只宠物:发现狗,Puff the Cat和Yertle 乌龟。当Puff出生时,现货已经岁了; Puff是p 当Yertle出生时岁月;当Yertle出现时,现货已经yy岁了 诞生了。 Spot的年龄,Puff的年龄和Yertle的年龄之和等于 迪克的年龄(d)和简的年龄(j)之和。 Spot,Puff多久了 还有Yertle?
给出的输入是s,p,y,j,所需的输出是:斑点的年龄,粉扑的年龄和yertle的年龄。
我的解决方案如下:
import sys
import math
d = 12
for line in sys.stdin:
line = [int(x) for x in line.strip("\n").split()]
s = line[0]
p = line[1]
y = line[2]
j = line[3]
yertle = (d+j-y-p)/3.0
difference = yertle - math.floor(yertle)
if difference > 0.5:
# for the 0.66666 cases
spot = puff = int(math.ceil(yertle+p))
yertle = int(math.floor(yertle))
else:
# for the 0.33333 cases
yertle = int(math.floor(yertle))
puff = yertle + p
spot = d+j - puff - yertle
print spot,puff,yertle
但在某些输入上不正确,例如:s = 5,p = 5,y = 10,j = 10。因为对于那些规格,狗的实际年龄是:spot = 12.333,puff = 7.333,yertle = 2.333但是因为我们正在进行整数除法,我们得到12,7,2。但是,这些结果不满足$$ spot + puff + yertle = dick + jane $$ 规则。有没有人对我犯错误的地方或我应该如何处理/解决这个问题有其他想法?
答案 0 :(得分:2)
不要使用浮点算法,使用整数。
让我们表示function loadMapsApi () {
if (navigator.connection.type === Connection.NONE || (global.google !== undefined && global.google.maps)) {
return;
}
// load maps api
}
,点的年龄D+J = DJ
,Puff的年龄S
,Yertle的年龄P
让我们的现场生日时间为零,所以Puff出生在区间Y
,Yertle出生在区间[s, s+1)
。当前时间是[y, y+1)
区间。
You can read up on it more here
如果我们查看时间线,我们可以看到
[S, S+1)
年龄总和是
S = y + Y
or
S = y + Y + 1
and
S = s + P
or
S = s + P + 1
其中(0,1,2)可能是附录
DJ = S + Y + P = S + S - y + S - s - (0, 1, 2)
我们可以看到正确的部分必须可以整除 3,所以下一次计算依赖于值
3 * S = DJ + y + s + (0,1,2)