📜  从B连续减少A或从A连续减少B使其变成(1,1)的次数

📅  最后修改于: 2021-04-22 02:04:17             🧑  作者: Mango

给定两个整数AB ,任务是找到将这对更改为(1,1)所需的最小操作数。在每个操作中,可以将(A,B)更改为(A – B,B),其中A>B。

注意:如果没有可能的解决方案,请打印(-1,1)。
例子:

天真的方法:想法是使用递归并将对更新为(A,A – B),其中A> B并将所需的操作数增加1。如果在任何步骤,该对中的任何元素小于1那么就不可能达到(1,1)。

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum number of operations
// required to reach (1, 1)
#include 
using namespace std;
      
// Function to find the minimum
// number of steps required
int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
        return -1;
          
    // Condition to check if the 
    // pair is reached to 1, 1
    if(a == 1 && b == 1)
        return c;
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
      
    return minimumSteps(a - b, b, c + 1);
} 
  
// Driver Code
int main()
{
    int a = 75;
    int b = 17;
      
    cout << minimumSteps(a, b, 0) << endl;
}
  
// This code is contributed by AbhiThakur


Java
// Java implementation to find the
// minimum number of operations
// required to reach (1, 1)
class GFG{
      
// Function to find the minimum
// number of steps required
static int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
        return -1;
          
    // Condition to check if the 
    // pair is reached to 1, 1
    if(a == 1 && b == 1)
        return c;
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
      
    return minimumSteps(a - b, b, c + 1);
} 
  
// Driver Code
public static void main(String []args)
{
    int a = 75;
    int b = 17;
      
    System.out.println(minimumSteps(a, b, 0));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the
# minimum number of operations
# required to reach (1, 1)
  
# Function to find the minimum
# number of steps required
def minimumSteps(a, b, c):
      
    # Conditon to check if it 
    # is not possible to reach
    if a < 1 or b < 1:
        return -1
          
    # Condition to check if the 
    # pair is reached to 1, 1
    if a == 1 and b == 1:
        return c
      
    # Condition to change the 
    # A as the maximum element
    if a < b:
        a, b = b, a
      
    return minimumSteps(a-b, b, c + 1)
      
# Driver Code
if __name__ == "__main__":
    a = 75; b = 17
      
    print(minimumSteps(a, b, 0))


C#
// C# implementation to find the
// minimum number of operations
// required to reach (1, 1)
using System;
  
class GFG{
      
// Function to find the minimum
// number of steps required
static int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
       return -1;
          
    // Condition to check if the 
    // pair is reached to 1, 1
    if(a == 1 && b == 1)
       return c;
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
    return minimumSteps(a - b, b, c + 1);
} 
  
// Driver Code
public static void Main()
{
    int a = 75;
    int b = 17;
      
    Console.WriteLine(minimumSteps(a, b, 0));
}
}
  
// This code is contributed by AbhiThakur


C++
// C++ implementation to find the
// minimum number of operations
// required to reach (1, 1)
#include 
using namespace std;
  
// Function to find the minimum
int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
    {
        return -1;
    }
      
    // Condition to check if the 
    // pair is reached to 1, 1
    if(min(a, b) == 1)
    {
        return c + max(a, b) - 1;
    }
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        swap(a, b);
    }
      
    return minimumSteps(a % b, b, c + (a / b));
}
  
// Driver Code
int main()
{
    int a = 75, b = 17;
    cout << minimumSteps(a, b, 0) << endl;
  
    return 0;
}
  
// This code is contributed by rutvik_56


Java
// Java implementation to find the
// minimum number of operations
// required to reach (1, 1)
import java.util.*;
class GFG{
  
// Function to find the minimum
static int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
    {
        return -1;
    }
      
    // Condition to check if the 
    // pair is reached to 1, 1
    if(Math.min(a, b) == 1)
    {
        return c + Math.max(a, b) - 1;
    }
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
      
    return minimumSteps(a % b, b, c + (a / b));
}
  
// Driver Code
public static void main(String[] args)
{
    int a = 75, b = 17;
    System.out.print(
           minimumSteps(a, b, 0) + "\n");
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation to find the
# minimum number of operations
# required to reach (1, 1)
  
# Function to find the minimum
# number of steps required
def minimumSteps(a, b, c):
      
    # Conditon to check if it 
    # is not possible to reach
    if a < 1 or b < 1:
        return -1
      
    # Condition to check if the 
    # pair is reached to 1, 1
    if min(a, b) == 1:
        return c + max(a, b) - 1
          
    # Condition to change the 
    # A as the maximum element
    if a < b:
        a, b = b, a
          
    return minimumSteps(a % b, b, c + a//b)
      
# Driver Code
if __name__ == "__main__":
    a = 75; b = 17
      
    print(minimumSteps(a, b, 0))


C#
// C# implementation to find the
// minimum number of operations
// required to reach (1, 1)
using System;
class GFG{
  
// Function to find the minimum
static int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
    {
        return -1;
    }
      
    // Condition to check if the 
    // pair is reached to 1, 1
    if(Math.Min(a, b) == 1)
    {
        return c + Math.Max(a, b) - 1;
    }
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
      
    return minimumSteps(a % b, b, c + (a / b));
}
  
// Driver Code
public static void Main()
{
    int a = 75, b = 17;
    Console.Write(minimumSteps(a, b, 0) + "\n");
}
}
  
// This code is contributed by Nidhi_biet


输出:
10

有效方法:想法是使用欧几里得算法来解决此问题。通过这种方法,我们可以按A / B步骤从(A,B)转到(A%B,B) 。但是,如果A和B的最小值为1,那么我们可以以A – 1的步长达到(1,1)。

下面是上述方法的实现:

C++

// C++ implementation to find the
// minimum number of operations
// required to reach (1, 1)
#include 
using namespace std;
  
// Function to find the minimum
int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
    {
        return -1;
    }
      
    // Condition to check if the 
    // pair is reached to 1, 1
    if(min(a, b) == 1)
    {
        return c + max(a, b) - 1;
    }
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        swap(a, b);
    }
      
    return minimumSteps(a % b, b, c + (a / b));
}
  
// Driver Code
int main()
{
    int a = 75, b = 17;
    cout << minimumSteps(a, b, 0) << endl;
  
    return 0;
}
  
// This code is contributed by rutvik_56

Java

// Java implementation to find the
// minimum number of operations
// required to reach (1, 1)
import java.util.*;
class GFG{
  
// Function to find the minimum
static int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
    {
        return -1;
    }
      
    // Condition to check if the 
    // pair is reached to 1, 1
    if(Math.min(a, b) == 1)
    {
        return c + Math.max(a, b) - 1;
    }
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
      
    return minimumSteps(a % b, b, c + (a / b));
}
  
// Driver Code
public static void main(String[] args)
{
    int a = 75, b = 17;
    System.out.print(
           minimumSteps(a, b, 0) + "\n");
}
}
  
// This code is contributed by sapnasingh4991

Python3

# Python3 implementation to find the
# minimum number of operations
# required to reach (1, 1)
  
# Function to find the minimum
# number of steps required
def minimumSteps(a, b, c):
      
    # Conditon to check if it 
    # is not possible to reach
    if a < 1 or b < 1:
        return -1
      
    # Condition to check if the 
    # pair is reached to 1, 1
    if min(a, b) == 1:
        return c + max(a, b) - 1
          
    # Condition to change the 
    # A as the maximum element
    if a < b:
        a, b = b, a
          
    return minimumSteps(a % b, b, c + a//b)
      
# Driver Code
if __name__ == "__main__":
    a = 75; b = 17
      
    print(minimumSteps(a, b, 0))

C#

// C# implementation to find the
// minimum number of operations
// required to reach (1, 1)
using System;
class GFG{
  
// Function to find the minimum
static int minimumSteps(int a, int b, int c)
{
      
    // Conditon to check if it 
    // is not possible to reach
    if(a < 1 || b < 1)
    {
        return -1;
    }
      
    // Condition to check if the 
    // pair is reached to 1, 1
    if(Math.Min(a, b) == 1)
    {
        return c + Math.Max(a, b) - 1;
    }
      
    // Condition to change the 
    // A as the maximum element
    if(a < b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
      
    return minimumSteps(a % b, b, c + (a / b));
}
  
// Driver Code
public static void Main()
{
    int a = 75, b = 17;
    Console.Write(minimumSteps(a, b, 0) + "\n");
}
}
  
// This code is contributed by Nidhi_biet
输出:
10