📌  相关文章
📜  将2、3或5的除法最小化以使两个给定的整数相等

📅  最后修改于: 2021-04-17 18:23:15             🧑  作者: Mango

给定两个整数XY中,任务是使XY等于通过将XY的由2,3,5倍最小数目,如果发现被整除。如果可以使两个整数相等,则打印“ -1”

例子:

方法:可以贪婪地解决给定的问题。想法是将给定的整数减少到其GCD,以使它们相等。请按照以下步骤解决问题:

  • 查找XY的最大公约数( GCD ),然后将XY除以它们的GCD
  • 初始化一个变量,例如count ,以存储所需的除法数。
  • 迭代直到X不等于Y并执行以下步骤:
    • 如果X的值大于Y ,则交换XY。
    • 如果较大数目(即,Y)2整除,3个,或5,则通过数除以它。增量计数1 。否则,如果不可能使两个数字相等,则打印“ -1”并退出循环。
  • 完成上述步骤后,如果两个数字都可以相等,则将count的值打印为使XY相等所需的最小除法数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate
// GCD of two numbers
int gcd(int a, int b)
{
    // Base Case
    if (b == 0) {
        return a;
    }
 
    // Calculate GCD recursively
    return gcd(b, a % b);
}
 
// Function to count the minimum
// number of divisions required
// to make X and Y equal
void minimumOperations(int X, int Y)
{
    // Calculate GCD of X and Y
    int GCD = gcd(X, Y);
 
    // Divide X and Y by their GCD
    X = X / GCD;
    Y = Y / GCD;
 
    // Stores the number of divisions
    int count = 0;
 
    // Iterate until X != Y
    while (X != Y) {
 
        // Maintain the order X <= Y
        if (Y > X) {
            swap(X, Y);
        }
 
        // If X is divisible by 2,
        // then divide X by 2
        if (X % 2 == 0) {
            X = X / 2;
        }
 
        // If X is divisible by 3,
        // then divide X by 3
        else if (X % 3 == 0) {
            X = X / 3;
        }
 
        // If X is divisible by 5,
        // then divide X by 5
        else if (X % 5 == 0) {
            X = X / 5;
        }
 
        // If X is not divisible by
        // 2, 3, or 5, then print -1
        else {
            cout << "-1";
            return;
        }
 
        // Increment count by 1
        count++;
    }
 
    // Print the value of count as the
    // minimum number of operations
    cout << count;
}
 
// Driver Code
int main()
{
    int X = 15, Y = 20;
    minimumOperations(X, Y);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to calculate
// GCD of two numbers
static int gcd(int a, int b)
{
     
    // Base Case
    if (b == 0)
    {
        return a;
    }
 
    // Calculate GCD recursively
    return gcd(b, a % b);
}
 
// Function to count the minimum
// number of divisions required
// to make X and Y equal
static void minimumOperations(int X, int Y)
{
     
    // Calculate GCD of X and Y
    int GCD = gcd(X, Y);
 
    // Divide X and Y by their GCD
    X = X / GCD;
    Y = Y / GCD;
 
    // Stores the number of divisions
    int count = 0;
 
    // Iterate until X != Y
    while (X != Y)
    {
         
        // Maintain the order X <= Y
        if (Y > X)
        {
            int t = X;
            X = Y;
            Y = t;
        }
 
        // If X is divisible by 2,
        // then divide X by 2
        if (X % 2 == 0)
        {
            X = X / 2;
        }
 
        // If X is divisible by 3,
        // then divide X by 3
        else if (X % 3 == 0)
        {
            X = X / 3;
        }
 
        // If X is divisible by 5,
        // then divide X by 5
        else if (X % 5 == 0)
        {
            X = X / 5;
        }
 
        // If X is not divisible by
        // 2, 3, or 5, then print -1
        else
        {
            System.out.print("-1");
            return;
        }
 
        // Increment count by 1
        count += 1;
    }
 
    // Print the value of count as the
    // minimum number of operations
    System.out.println(count);
}
 
// Driver Code
static public void main(String args[])
{
    int X = 15, Y = 20;
     
    minimumOperations(X, Y);
}
}
 
// This code is contributed by ipg2016107


Python3
# Python3 program for the above approach
 
# Function to calculate
# GCD of two numbers
def gcd(a, b):
     
    # Base Case
    if (b == 0):
        return a
 
    # Calculate GCD recursively
    return gcd(b, a % b)
 
# Function to count the minimum
# number of divisions required
# to make X and Y equal
def minimumOperations(X, Y):
     
    # Calculate GCD of X and Y
    GCD = gcd(X, Y)
 
    # Divide X and Y by their GCD
    X = X // GCD
    Y = Y // GCD
 
    # Stores the number of divisions
    count = 0
 
    # Iterate until X != Y
    while (X != Y):
 
        # Maintain the order X <= Y
        if (Y > X):
            X, Y = Y, X
 
        # If X is divisible by 2,
        # then divide X by 2
        if (X % 2 == 0):
            X = X // 2
 
        # If X is divisible by 3,
        # then divide X by 3
        elif (X % 3 == 0):
            X = X // 3
 
        # If X is divisible by 5,
        # then divide X by 5
        elif (X % 5 == 0):
            X = X // 5
             
        # If X is not divisible by
        # 2, 3, or 5, then pr-1
        else:
            print("-1")
            return
 
        # Increment count by 1
        count += 1
 
    # Print the value of count as the
    # minimum number of operations
    print (count)
 
# Driver Code
if __name__ == '__main__':
     
    X, Y = 15, 20
     
    minimumOperations(X, Y)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
// Function to calculate
// GCD of two numbers
static int gcd(int a, int b)
{
   
    // Base Case
    if (b == 0) {
        return a;
    }
 
    // Calculate GCD recursively
    return gcd(b, a % b);
}
 
// Function to count the minimum
// number of divisions required
// to make X and Y equal
static void minimumOperations(int X, int Y)
{
   
    // Calculate GCD of X and Y
    int GCD = gcd(X, Y);
 
    // Divide X and Y by their GCD
    X = X / GCD;
    Y = Y / GCD;
 
    // Stores the number of divisions
    int count = 0;
 
    // Iterate until X != Y
    while (X != Y) {
 
        // Maintain the order X <= Y
        if (Y > X) {
            int t = X;
            X = Y;
            Y = t;
        }
 
        // If X is divisible by 2,
        // then divide X by 2
        if (X % 2 == 0) {
            X = X / 2;
        }
 
        // If X is divisible by 3,
        // then divide X by 3
        else if (X % 3 == 0) {
            X = X / 3;
        }
 
        // If X is divisible by 5,
        // then divide X by 5
        else if (X % 5 == 0) {
            X = X / 5;
        }
 
        // If X is not divisible by
        // 2, 3, or 5, then print -1
        else {
            Console.WriteLine("-1");
            return;
        }
 
        // Increment count by 1
        count++;
    }
 
    // Print the value of count as the
    // minimum number of operations
    Console.WriteLine(count);
}
 
// Driver Code
static public void Main()
{
    int X = 15, Y = 20;
    minimumOperations(X, Y);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
3

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