📌  相关文章
📜  从二维平面的原点到给定长度(d,0)的给定长度所需的跳数

📅  最后修改于: 2021-04-27 09:37:15             🧑  作者: Mango

给定三个正整数a,bd 。您当前位于无限2D坐标平面上的原点(0,0)。您可以从当前位置以等于或等于ab的欧几里得距离跳到2D平面上的任何点。任务是找到从(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)是一个欧几里得距离,我们将跳一个长度为a的跳跃并到达(d,0)。因此,在这种情况下,所需的最小跳转数将增加1。
现在,让我们解决剩余距离不等于的情况。令剩下的距离为x 。同样,观察x将大于0且小于max(a,b) ,因此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