给定两个整数X和Y中,任务是使X和Y等于通过将X或Y的由2,3,或5倍最小数目,如果发现被整除。如果可以使两个整数相等,则打印“ -1” 。
例子:
Input: X = 15, Y = 20
Output: 3
Explanation:
Operation 1: Reduce X(= 15) to 15/3 i.e., X = 15/3 = 5.
Operation 2: Reduce Y(= 20) to 20/2 i.e., Y = 20/2 = 10.
Operation 3: Reduce Y(= 20) to 10/2 i.e., Y = 10/2 = 5.
After the above operations, X and Y are equal i.e., 5.
Therefore, the count of operation required is 3.
Input: X = 6, Y = 6
Output: 0
方法:可以贪婪地解决给定的问题。想法是将给定的整数减少到其GCD,以使它们相等。请按照以下步骤解决问题:
- 查找X和Y的最大公约数( GCD ),然后将X和Y除以它们的GCD 。
- 初始化一个变量,例如count ,以存储所需的除法数。
- 迭代直到X不等于Y并执行以下步骤:
- 如果X的值大于Y ,则交换X和Y。
- 如果较大数目(即,Y)被2整除,3个,或5,则通过数除以它。增量计数为1 。否则,如果不可能使两个数字相等,则打印“ -1”并退出循环。
- 完成上述步骤后,如果两个数字都可以相等,则将count的值打印为使X和Y相等所需的最小除法数。
下面是上述方法的实现:
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)