给定三个正整数a, b和d 。您当前位于无限二维坐标平面上的原点 (0, 0)。您可以在 2D 平面上的任何点上跳跃,距离等于a或b 的欧几里得距离当前位置。任务是找到从 (0, 0) 到达 (d, 0) 所需的最小跳跃次数。
例子:
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
首先,观察我们不需要找到中间点,我们只想要所需的最少跳跃次数。
所以,从 (0, 0) 我们将在 (d, 0) 的方向上移动,即垂直跳跃长度等于max(a, b)直到当前位置坐标和 (d, 0) 之间的欧几里得距离变得小于max(a, b)或等于 0。这将使每次跳跃所需的最小跳跃次数增加 1。所以这个值可以通过floor(d/max(a, b)) 找到。
现在,对于距离的休息,如果(d,0)是欧几里得距离,我们将长度的一个跳跃,到达(d,0)。因此,在这种情况下,所需的最小跳转次数将增加 1。
现在,让我们解决剩余距离不等于a 的情况。让剩下的距离为x 。此外,观察 x 将大于 0 且小于max(a, b) ,因此,0 < x < 2*max(a, b)。我们可以通过 2 步到达 (d, 0)。
如何 ?
让我们尝试构建一个三角形 ABC,其中 A 是我们当前的位置,B 是目标位置,即 (d, 0),而 C 将是 AC = BC = max(a, b) 的点。这是可能的三角形,因为两条边 AC + BC 的总和大于第三边 AB。因此,一次跳转是从 A 到 C,另一次从 C 跳转到 B。
下面是这个方法的实现:
C++
#include
using namespace std;
// Return the minimum jump of length either a or b
// required to reach (d, 0) from (0, 0).
int minJumps(int a, int b, int d)
{
// Assigning maximum of a and b to b
// and assigning minimum of a and b to a.
int temp = a;
a = min(a, b);
b = max(temp, b);
// if d is greater than or equal to 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;
}
int main()
{
int a = 3, b = 4, d = 11;
cout << minJumps(a, b, d) << endl;
return 0;
}
Java
// Java code to find the minimum number
// of jump required to reach
// (d, 0) from (0, 0).
import java.io.*;
class GFG {
// Return the minimum jump of length either a or b
// required to reach (d, 0) from (0, 0).
static int minJumps(int a, int b, int d)
{
// Assigning maximum of a and b to b
// and assigning minimum of a and b to a.
int temp = a;
a = Math.min(a, b);
b = Math.max(temp, b);
// if d is greater than or equal to 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;
}
// Driver code
public static void main(String[] args)
{
int a = 3, b = 4, d = 11;
System.out.println(minJumps(a, b, d));
}
}
// This code is contributed by vt_m
Python3
# Python3 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
# Driver Code
a, b, d = 3, 4, 11
print (int(minJumps(a, b, d)))
# This code is contributed by _omg
C#
// C# code to find the minimum number
// of jump required to reach
// (d, 0) from (0, 0).
using System;
class GFG {
// Return the minimum jump of length either a or b
// required to reach (d, 0) from (0, 0).
static int minJumps(int a, int b, int d)
{
// Assigning maximum of a and b to b
// and assigning minimum of a and b to a.
int temp = a;
a = Math.Min(a, b);
b = Math.Max(temp, b);
// if d is greater than or equal to 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;
}
// Driver code
public static void Main()
{
int a = 3, b = 4, d = 11;
Console.WriteLine(minJumps(a, b, d));
}
}
// This code is contributed by vt_m
PHP
= $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;
}
// Driver Code
$a = 3;
$b = 4;
$d = 11;
echo floor(minJumps($a, $b, $d));
// This code is contributed by anuj_67.
?>
Javascript
输出:
3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。