【发布时间】:2024-12-24 07:25:01
【问题描述】:
问题如下:
迪克 d=12 岁。当我们这么说的时候,我们的意思是距离迪克出生至少十二年而不是十三年。
迪克和简养了三只宠物:Spot the dog、Puff the Cat 和 Yertle 乌龟。 Puff 出生时,Spot 才 3 岁;粉扑是 p 耶特尔出生时的岁数;当 Yertle 出生时,Spot 已经 yy 岁了 出生。 Spot 的年龄、Puff 的年龄和 Yertle 的年龄之和等于 迪克的年龄 (d) 和简的年龄 (j) 之和。 Spot、Puff 几岁了, 还有耶特尔?
给定的输入是 s,p,y,j,需要的输出是:spot 的年龄、puff 的年龄和 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$$ 规则。有没有人对我在哪里犯错或我应该如何处理/解决这个问题有其他想法?
【问题讨论】:
-
spot=13.000, puff=7.333, yertle=2.333 满足输入 s=5, p=5, y=10, j=10。 Spot 和 Yertle 的实际年龄差是 y + [0,1)
-
@hk6279 但是,Puff 和 Yertle 之间的区别不是类似,p + [0,1)。在这种情况下,我们可以说 spot=12.333,puff = 8,yertle=2.333,对吧?我们怎么知道将 0.6666 添加到 spot 而不是 puff 或 yertle?
-
同样的规则适用于 (Puff and Yertle) 和 (Puff and Spot)。但是 spot=12.333, puff = 8, yertle=2.333 不正确,因为 12.333-8 = 4.333。
-
@hk6279 啊,我明白你的意思了。我试图在我的代码的较新版本中解释这一点(现在粘贴在问题中)。例如,它在输入 0 1 1 10 上正确返回 8 8 6 (应该正确?)但我仍然得到错误的答案判决。有什么想法吗?
标签: python algorithm math equation-solving