两个兔子见面

2024-09-30 08:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我要解决的问题:

Given a straight line with the starting point "O". Two hares begin simultaneously moving along this straight line away from this point. They started at different distances from point O.

我需要在编程语言Python中创建一个函数,它将显示出兔子与O点的距离。在

有以下价值观:

  • 从O点开始的位置(几个单位)
  • 一次跳完的跳跃长度(几个单位)
  • 休息时间(睡眠)
  • 每跳一次,野兔必须休息(睡觉)。在

它显示兔子在两次跳跃之间休息的时间(多少时间单位)。跳跃本身持续0个单位。 众所周知,所有值都是整数:

  • 位置>=0
  • 跳跃长度>;=0
  • 休息时间(睡眠)>;=1

我需要编写一个函数,它使用两个hare的所有命名值,并且可以显示

  • -如果野兔永远不会相遇
  • 他们第一次见面的位置。会议在地面举行,但不是在休息的最后一秒。在

这是我的代码:

from fractions import Fraction

def meet_me(pos1, jump_distance1, sleep1, pos2, jump_distance2, sleep2):
        if pos1 == pos2:
            pos1 = pos2
        elif (jump_distance1 / sleep1 > jump_distance2 / sleep2 and pos1 > pos2) or (jump_distance2 / sleep2 > jump_distance1 / sleep1 and pos2 > pos1):
            pos1 = -1
        elif jump_distance1 / sleep1 == jump_distance2 / sleep2:
            pos1 = -1
        else:
            if pos1 > pos2:
                while pos1 != pos2:
                    pos1 += Fraction(jump_distance1, sleep1)
                    pos2 += Fraction(jump_distance2, sleep2)
                    if pos2 > pos1:
                        pos1 = -1
                        break
            elif pos2 > pos1:
                while pos1 != pos2:
                    pos1 += Fraction(jump_distance1, sleep1)
                    pos2 += Fraction(jump_distance2, sleep2)
                    if pos1 > pos2:
                        pos1 = -1
                        break
        return pos1

print(meet_me(1, 2, 1, 2, 1, 1))   # = > 3
print(meet_me(1, 2, 1, 1, 2, 1))  # => 3
print(meet_me(1, 2, 3, 4, 5, 5))  # => -1
print(meet_me(3, 5, 10, 4, 1, 2))  # => 8
print(meet_me(100, 7, 4, 300, 8, 6))  # => 940
print(meet_me(0, 1, 1, 1, 1, 1))  # => -1
print(meet_me(10, 7, 7, 5, 8, 6))   # => 45
print(meet_me(1, 7, 1, 15, 5, 1))   # => 50

我的预期输出是:

^{pr2}$

我不明白我做错了什么。这是我得到的输出:

1
-1
-1
940
-1
25
50

Tags: fromif单位pointmeprintjumpfraction
2条回答

这是我的建议
但是,它不会返回期望值,但请阅读我对该主题的评论(我认为解决方案是错误的)

总之,
如果没有,您可以修改我的代码(类似的代码应该可以正常工作)

from fractions import Fraction

def meet_me(p1, x1, t1, p2, x2, t2):
    if p1 == p2:
        # if they are already on the same place, they met at that position
        return p1
    elif Fraction(x1, t1) == Fraction(x2, t2):
        # same speed but different positions like in 4th case (reed my comment about that)
        return -1

    if p2 < p1:
        # to ensure that 1st is lesser
        p2, p1 = p1, p2
        x2, x1 = x1, x2
        t2, t1 = t1, t2

    # simulation by seconds (it can also be by gcd(t1, t2))
    time = 1    # 1 or gcd assuming they do not jump instantly
    while p1 != p2:
        if not time % t2:
            if p2 < p1:
                #p2 < p1 which means speed1 > speed2 since at begining p1 < p2
                # 2nd will make even greater distance between that 1st won't catch up with
                return -1
            p2 += x2
        if not time % t1:
            p1 += x1
        time += 1 # or gcd
    return p1

我花了比我预期的时间更长的时间,但这是我的解决方案,它给出了你想要的答案。在

我看你有两个问题。首先,你要在它们移动之前检查它们是否相等,问题似乎表明这不应该发生

参见示例2:print(meet_me(1,2,1,1,2,1))#=>;3而不是1

第二个问题是,你把野兔当作一个不断移动的过程,而不是像问题所要求的那样移动然后休息。一只跑得慢的兔子可能落后于跑得快的兔子,在被甩在后面之前会超过它几次,所以你必须做一个更复杂的检查,看看它们是否永远不会相遇。在

from fractions import Fraction

def meet_me(pos1, jump_distance1, sleep1, pos2, jump_distance2, sleep2):

        speed_1 = Fraction(jump_distance1, sleep1)
        speed_2 = Fraction(jump_distance2, sleep2)

        # Check which hare is slower. If equal speed, set slower_hare to 1.
        if speed_2 >= speed_1:
            slower_hare = 1
        else:
            slower_hare = 2

        distance_between_hares = None

        slower_just_moved = False

        current_position_1 = pos1
        current_position_2 = pos2

        # How long do they have left to sleep?
        current_sleep_remaining_1 = 0
        current_sleep_remaining_2 = 0


        # Do the loop.
        while True:
            # Check if the hares are still sleeping, and if not, jump and reset the sleep timer.
            if current_sleep_remaining_1 == 0:
                current_position_1 += jump_distance1
                current_sleep_remaining_1 = sleep1
                if slower_hare == 1:
                    slower_just_moved = True


            if current_sleep_remaining_2 == 0:
                current_position_2 += jump_distance2
                current_sleep_remaining_2 = sleep2
                if slower_hare == 2:
                    slower_just_moved = True


            current_sleep_remaining_1 -= 1
            current_sleep_remaining_2 -= 1


            # Check to see if they're at the same position.
            if current_position_1 == current_position_2:
                return current_position_1

            # Check if they will never meet. If the slower hare is behind the faster one, and after moving gets further behind,
            # or stays same distance as it was the previous time it jumped, then it will never catch up.
            if slower_just_moved:
                if slower_hare == 1:
                    if current_position_1 < current_position_2:
                        previous_distance_between_hares = distance_between_hares
                        distance_between_hares = current_position_2 - current_position_1
                        if previous_distance_between_hares != None and distance_between_hares >= previous_distance_between_hares:
                            return -1
                if slower_hare == 2:
                    if current_position_2 < current_position_1:
                        previous_distance_between_hares = distance_between_hares
                        distance_between_hares = current_position_1 - current_position_2
                        if previous_distance_between_hares != None and distance_between_hares >= previous_distance_between_hares:
                            return -1

            slower_just_moved = False

相关问题 更多 >

    热门问题