【问题标题】:The D+J programming challenge from KattisKattis 的 D+J 编程挑战
【发布时间】: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$$ 规则。有没有人对我在哪里犯错或我应该如何处理/解决这个问题有其他想法?

附: link for problem source

【问题讨论】:

  • 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


【解决方案1】:

不要使用浮点运算,使用整数。

让我们表示D+J = DJ,Spot 的年龄 S,Puff 的年龄 P,Yertle 的年龄 Y

Let's Spot 生日时间为零,所以 Puff 出生在区间 [s, s+1),Yertle 出生在区间 [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

【讨论】:

  • 感谢您的全面回答!您能否详细说明最后一个案例,题为“现在更复杂的案例......”;就像 y = s + p + 1 的恒等式是从哪里来的一样,我注意到你在 Y 之前先计算 S,而我一直在先计算 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,然后求解与 Y 相关的 S 和 P。
  • 我为 Spot 编写了方程,因为它对我来说似乎更简单。如果您可以先为 Y 制定简单而正确的方法 - 使用它。
最近更新 更多