Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python D+;J来自Kattis的编程挑战_Python_Algorithm_Math_Equation Solving - Fatal编程技术网

Python D+;J来自Kattis的编程挑战

Python D+;J来自Kattis的编程挑战,python,algorithm,math,equation-solving,Python,Algorithm,Math,Equation Solving,问题如下: 迪克12岁。当我们这样说的时候,我们的意思是,迪克出生至少十二年了,而不是十三年 迪克和简有三只宠物:斑点狗、帕夫猫和耶特尔 乌龟。波夫出生时,斯波特只有s岁;帕夫是p 耶特尔出生时,年;耶特尔出生时,斯波特只有yy岁 出生的。Spot年龄、Puff年龄和Yertle年龄之和等于 迪克的年龄(d)和简的年龄(j)之和。Spot,Puff,你几岁了, 耶特尔呢 给定的输入是s、p、y、j,所需的输出是:spot的年龄、puff的年龄和yertle的年龄 我的解决方案如下: import

问题如下:

迪克12岁。当我们这样说的时候,我们的意思是,迪克出生至少十二年了,而不是十三年

迪克和简有三只宠物:斑点狗、帕夫猫和耶特尔 乌龟。波夫出生时,斯波特只有s岁;帕夫是p 耶特尔出生时,年;耶特尔出生时,斯波特只有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$$ 规则。有没有人对我在哪里犯了错误或者我应该如何处理/解决这个问题有其他想法


注意:不要使用浮点运算,使用整数

让我们来表示
D+J=DJ
,Spot的年龄
s
,Puff的年龄
p
,Yertle的年龄
Y

让我们发现生日时间是零,所以帕夫出生在区间
[s,s+1)
,耶特尔出生在区间
[y,y+1)
。当前时间在区间
[s,s+1)

如果我们看时间线,我们可以看到

   S = y + Y
   or
   S = y + Y + 1
and 
   S = s + P
   or
   S = s + P + 1
年龄总和为

 DJ = S + Y + P = S + S - y + S - s - (0, 1, 2)
其中(0,1,2)是可能的

 3 * S = DJ + y + s + (0,1,2)
我们可以看到右边的部分可以被3整除,所以下一步的计算取决于这个值

 M =  (DJ + y + s) modulo 3

case M = 0: (5 5 10 9)
     S = (DJ + y + s) / 3 = (21 + 15) / 3 = 12
     P = S - s = 12 - 5 = 7
     Y = S - y = 12 - 10 = 2

case M = 1: (5 5 10 10)
     here we should add 2 to make sum 37 divisible by 3
     S = (DJ + y + s + 2) / 3 = (22 + 15 + 2) / 3 = 13
     P = S - s  - 1 = 13 - 5 = 1 =  7
     Y = S - y  - 1 = 13 - 10 - 1 = 2

now more complex case M = 2 (5 5 11 10):
    here we should add 1 to make sum 38 divisible by 3 
    and solve - where use 1 - for P or for Y calculation?
    We can determine this evaluating s/p/y relation:
    if y = s + p + 1 then use 1 for Puff's age else for Yertle
    (because Puff's fraction is larger then Yertle's fraction, 
    she was born in the later year period)
    here 11 = 5 + 5 + 1, so
    S = (22 + 16 + 1) / 3 = 13
    Y = S - y = 13 - 11 = 2
    P = S - s - 1 = 13 - 5 - 1 = 7

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和yertle)和(puff和spot)。但是spot=12.333,puff=8,yertle=2.333是不正确的,因为12.333-8=4.333。@hk6279啊,我明白你的意思。我试着在我的代码的较新版本(现在粘贴在问题中)中对此进行解释。例如,它正确地返回了输入0 1 10上的8 6(应该正确吗?)但我还是得到了一个错误的答案。有什么想法吗?谢谢你的全面回答!你能详细说明最后一个案例吗,题为“现在更复杂的案例…”的案例;比如y=s+p+1恒等式是从哪里来的,我注意到你先计算y,而我先计算y,这有区别吗假设Spot出生于2000年1月,Puff出生于2005年10月,Yertle出生于2011年2月。s=5,y=11,p=5!2013年5月,Puff的年龄为7岁(而2013-2005年为8岁).在回答中添加了一些注释。我尝试实现了你的算法,它成功了!但请为我回答这个问题:你先解Spot的年龄,然后解与其相关的Y和P,这有关系吗?因为我先解Yertle,然后解s和P,再解其相关的Y。我为Spot编写了方程,因为它对我来说似乎更简单。如果您可以先为Y制定简单正确的方法-使用它。