📌  相关文章
📜  从(X,Y)达到(1,1)所需的最小给定移动次数

📅  最后修改于: 2021-04-17 17:53:21             🧑  作者: Mango

给定两个整数XY ,任务是计算从点(1,1)开始到达点(1,1)所需的最小移动数(如果允许的话) ,例如(a, b)(a – b,b)(a,b – a)。

例子:

天真的方法:解决问题的最简单方法是不断从较大的数中减去较小的数,直到它们相等为止。如果在任何时刻XY小于1 ,则打印-1 。否则,将执行的减法次数打印为所需的最小操作数。

时间复杂度: O(max(X,Y))
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是通过取模运算来替换重复的减法,这与使用欧几里得算法计算GCD相似。请按照以下步骤解决问题:

  • 初始化一个变量,例如用0表示cnt 以存储最少的步骤数。
  • XY都不为零时进行迭代,并执行以下操作:
    • 如果X> Y的值则将X / Y添加到cnt 。更新至X%Y
    • 如果Y> X的值则将Y / X添加到cnt 。更新yY%X
  • 检查其中之一是否大于1 ,然后打印-1
  • 否则,将cnt的值减小1,因为需要采取额外的步骤以使其中之一等于0
  • 打印cnt的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of steps
// required to convert (x, y) to (1, 1)
int minimumSteps(int x, int y)
{
    // Store the required result
    int cnt = 0;
 
    // Iterate while both x
    // and y are not equal to 0
    while (x != 0 && y != 0) {
 
        // If x is greater than y
        if (x > y) {
 
            // Update count and value of x
            cnt += x / y;
            x %= y;
        }
 
        // Otherwise
        else {
 
            // Update count and value of y
            cnt += y / x;
            y %= x;
        }
    }
    cnt--;
 
    // If both x and y > 1
    if (x > 1 || y > 1)
        cnt = -1;
 
    // Print the result
    cout << cnt;
}
 
// Driver Code
int main()
{
    // Given X and Y
    int x = 3, y = 1;
    minimumSteps(x, y);
 
    return 0;
}


Java
// Java program for above approach
import java.util.*;
 
class GFG{
 
// Function to count the number of steps
// required to convert (x, y) to (1, 1)
static void minimumSteps(int x, int y)
{
     
    // Store the required result
    int cnt = 0;
 
    // Iterate while both x
    // and y are not equal to 0
    while (x != 0 && y != 0)
    {
         
        // If x is greater than y
        if (x > y)
        {
             
            // Update count and value of x
            cnt += x / y;
            x %= y;
        }
 
        // Otherwise
        else
        {
             
            // Update count and value of y
            cnt += y / x;
            y %= x;
        }
    }
    cnt--;
 
    // If both x and y > 1
    if (x > 1 || y > 1)
        cnt = -1;
 
    // Print the result
    System.out.println(cnt);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given X and Y
    int x = 3, y = 1;
     
    minimumSteps(x, y);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to count the number of steps
# required to convert (x, y) to (1, 1)
def minimumSteps(x, y):
     
    # Store the required result
    cnt = 0
 
    # Iterate while both x
    # and y are not equal to 0
    while (x != 0 and y != 0):
         
        # If x is greater than y
        if (x > y):
             
            # Update count and value of x
            cnt += x / y
            x %= y
 
        # Otherwise
        else:
             
            # Update count and value of y
            cnt += y / x
            y %= x
             
    cnt -= 1
 
    # If both x and y > 1
    if (x > 1 or y > 1):
        cnt = -1
 
    # Print the result
    print(int(cnt))
 
# Driver Code
if __name__ == '__main__':
     
    # Given X and Y
    x = 3
    y = 1
     
    minimumSteps(x, y)
     
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of steps
// required to convert (x, y) to (1, 1)
public static void minimumSteps(int x, int y)
{
     
    // Store the required result
    int cnt = 0;
 
    // Iterate while both x
    // and y are not equal to 0
    while (x != 0 && y != 0)
    {
         
        // If x is greater than y
        if (x > y)
        {
             
            // Update count and value of x
            cnt += x / y;
            x %= y;
        }
 
        // Otherwise
        else
        {
             
            // Update count and value of y
            cnt += y / x;
            y %= x;
        }
    }
    cnt--;
 
    // If both x and y > 1
    if (x > 1 || y > 1)
        cnt = -1;
 
    // Print the result
    Console.WriteLine(cnt);
}
 
// Driver Code
public static void Main()
{
     
    // Given X and Y
    int x = 3, y = 1;
     
    minimumSteps(x, y);
}
}
 
// This code is contributed by mohit kumar 29


输出:
2

时间复杂度: O(log(max(X(Y,Y))))
辅助空间: O(1)