这是我要解决的问题:
给出一条起始点为“ O”的直线。两个野兔开始同时沿着远离该点的直线移动。他们开始于与O点不同的距离。
我需要使用Python编程语言创建一个函数,该函数将显示野兔离O点的距离。
有值:
它显示野兔在两次跳跃之间要休息多长时间(多少时间单位)。跳跃本身持续0个单位。 众所周知,所有值都是整数:
我需要编写一个函数,将所有野兔都使用所有命名的值并可以显示
这是我的代码:
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
这是我的预期输出:
3
3
-1
8
940
-1
45
50
我不明白我在做什么错。这是我得到的输出:
1
-1
-1
940
-1
25
50
答案 0 :(得分:0)
这是我的建议
但是,它不会返回期望值,而是请阅读我对该主题的评论(我认为解决方案是错误的)
反正
如果没有,您可以修改我的代码(类似的代码应该可以正常工作)
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
答案 1 :(得分:0)
花了我比预期更长的时间,但这是我的解决方案,可以为您提供所需的答案。
我有两个问题。首先是您要在移动之前检查它们是否相等,这似乎表明该问题不应发生;
请参阅示例2:print(meet_me(1、2、1、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