Python程序,用于给定长度的跳转次数,以从二维平面的原点到达形式点(d,0)

给定三个正整数 a、 b D .您当前位于无限二维坐标平面上的原点(0,0)。你可以跳到二维平面上的任意点,欧几里德距离等于 A. B 从你现在的位置。任务是找到从(0,0)到达(d,0)所需的最小跳跃次数。

null

例如:

Input : a = 2, b = 3, d = 1 
Output : 2
First jump of length a = 2, (0, 0) -> (1/2, √15/2)
Second jump of length a = 2, (1/2, √15/2) -> (1, 0)
Thus, only two jump are required to reach 
(1, 0) from (0, 0).

Input : a = 3, b = 4, d = 11 
Output : 3
(0, 0) -> (4, 0) using length b = 4
(4, 0) -> (8, 0) using length b = 4
(8, 0) -> (11, 0) using length a = 3

# Python code to find the minimum number
# of jump required to reach
# (d, 0) from (0, 0)
def minJumps(a, b, d):
temp = a
a = min (a, b)
b = max (temp, b)
if (d > = b):
return (d + b - 1 ) / b
# if d is 0
if (d = = 0 ):
return 0
# if d is equal to a.
if (d = = a):
return 1
# else make triangle, and only 2
# steps required.
return 2
# main()
a = 3
b = 4
d = 11
print ( int (minJumps(a, b, d)))
# Contributed by _omg


输出

3

请参阅完整的文章 从二维平面原点到达形状点(d,0)所需的给定长度的跳跃次数 更多细节!

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享