📌  相关文章
📜  使两个数相等所需的最少操作

📅  最后修改于: 2021-04-26 09:01:09             🧑  作者: Mango

给定两个整数AB。任务是找到使A和B相等所需的最小操作数。在每个操作中,可以执行以下任一步骤:

  • 用其初始值递增A或B。
  • 将A和B都增加其初始值

例子:

方法:可以使用GCD解决此问题。

  1. 如果A大于B,则交换A和B。
  2. 现在减少B,使A和B的gcd变为1。
  3. 因此,达到相等值所需的最小操作为(B – 1)。

例如:如果A = 4,B = 10:

  • 第1步:比较4和10,因为我们始终需要B作为更大的值。在这里,B已经大于A。因此,现在不需要交换。
  • 步骤2: GCD(4,10)=2。因此,我们将B减小为B / 2。现在,A = 4,B = 5。
    GCD(4,5)= 1,这是目标。
  • 步骤3: (B – 1的当前值)将是所需的计数。此处,当前B =5。因此(5 – 1 = 4),即总共需要进行4次操作。

下面是上述方法的实现。

C++
// C++ program to find minimum
// operations required to
// make two numbers equal
  
#include 
using namespace std;
  
// Function to return the
// minimum operations required
long long int minOperations(
    long long int A,
    long long int B)
{
  
    // Keeping B always greater
    if (A > B)
        swap(A, B);
  
    // Reduce B such that
    // gcd(A, B) becomes 1.
    B = B / __gcd(A, B);
  
    return B - 1;
}
  
// Driver code
int main()
{
    long long int A = 7, B = 15;
  
    cout << minOperations(A, B)
         << endl;
  
    return 0;
}


Java
// Java program to find minimum
// operations required to
// make two numbers equal
class GFG{
   
// Function to return the
// minimum operations required
static int  minOperations(
    int  A,
    int  B)
{
   
    // Keeping B always greater
    if (A > B) {
        A = A+B;
        B = A-B;
        A = A-B;
    }
   
    // Reduce B such that
    // gcd(A, B) becomes 1.
    B = B / __gcd(A, B);
   
    return B - 1;
}
static int __gcd(int a, int b)  
{  
    return b == 0? a:__gcd(b, a % b);     
} 
  
// Driver code
public static void main(String[] args)
{
    int  A = 7, B = 15;
   
    System.out.print(minOperations(A, B)
         +"\n");
   
}
}
  
// This code contributed by sapnasingh4991


Python3
# Python program to find minimum 
# operations required to 
# make two numbers equal 
import math
  
# Function to return the 
# minimum operations required 
def minOperations(A, B):
  
    # Keeping B always greater 
    if (A > B):
        swap(A, B)
  
    # Reduce B such that 
    # gcd(A, B) becomes 1. 
    B = B // math.gcd(A, B); 
  
    return B - 1
  
# Driver code 
A = 7
B = 15
  
print(minOperations(A, B))
  
# This code is contributed by Sanjit_Prasad


C#
// C# program to find minimum
// operations required to
// make two numbers equal
using System;
  
class GFG{
    
// Function to return the
// minimum operations required
static int  minOperations(
    int  A,
    int  B)
{
    
    // Keeping B always greater
    if (A > B) {
        A = A+B;
        B = A-B;
        A = A-B;
    }
    
    // Reduce B such that
    // gcd(A, B) becomes 1.
    B = B / __gcd(A, B);
    
    return B - 1;
}
static int __gcd(int a, int b)  
{  
    return b == 0? a:__gcd(b, a % b);     
} 
   
// Driver code
public static void Main(String[] args)
{
    int  A = 7, B = 15;
    
    Console.Write(minOperations(A, B)
         +"\n");
}
}
  
// This code is contributed by sapnasingh4991


输出:
14

时间复杂度: O(log(max(A,B))