📜  通过重复除法和乘法将X转换为Y的最小步骤

📅  最后修改于: 2021-04-23 16:07:32             🧑  作者: Mango

给定两个整数XY ,任务是找到在每个步骤中使用任何操作将整数X转换为Y的最小步骤数:

  • 用数字除以任何自然数
  • 将数字乘以任何自然数

例子:

方法:解决上述问题:

  • 确保X包含X和Y中较小的值。现在,如果X大于Y,则我们知道将较小的数字更改为较大的数字总是比较容易。因此,我们只交换X和Y的值,然后执行下面提到的步骤。
  • 如果两个整数都相同,则答案将为零,因为不进行任何转换。
    例如,
If X = 4, Y = 4

Here 4 = 4
Therefore, answer = 0
(as they both are already same)
  • 但是,如果X小于Y,则:
    • 我们必须检查Y%X是否给出0。
    • 如果是,则Y可以表示为X *(Y / X) ,我们一步就可以得到所需的输出。
      例如,
If X = 4, Y = 12

Here 12 % 4 = 0
Therefore, answer = 1
(4 * 3 = 12)
  • 否则,答案将为2,因为它需要两个步骤,一个步骤用于除法(X = X / X),另一个步骤用于相乘(X = X * Y)。
    例如,
If X = 8, Y = 13

Here 13 % 8 != 0
Therefore, 
1. X = X/X = 8/8 = 1
2. X = X*Y = 1*13 = 13

Hence, answer = 2

下面是上述方法的实现:

C++
// C++ implementation to find minimum
// steps to convert X to Y by repeated
// division and multiplication
 
#include 
using namespace std;
 
int solve(int X, int Y)
{
    // Check if X is greater than Y
    // then swap the elements
    if (X > Y) {
        int temp = X;
        X = Y;
        Y = temp;
    }
 
    // Check if X equals Y
    if (X == Y)
        cout << 0 << endl;
 
    else if (Y % X == 0)
        cout << 1 << endl;
    else
        cout << 2 << endl;
}
 
// Driver code
int main()
{
    int X = 8, Y = 13;
    solve(X, Y);
 
    return 0;
}


Java
// Java implementation to find minimum
// steps to convert X to Y by repeated
// division and multiplication
class GFG{
 
static int solve(int X, int Y)
{
    // Check if X is greater than Y
    // then swap the elements
    if (X > Y)
    {
        int temp = X;
        X = Y;
        Y = temp;
    }
 
    // Check if X equals Y
    if (X == Y)
        System.out.println(0 );
 
    else if (Y % X == 0)
        System.out.println( 1 );
    else
        System.out.println(2 );
        return 0;
}
 
// Driver code
public static void main(String args[])
{
    int X = 8, Y = 13;
    solve(X, Y);
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 implementation to find minimum
# steps to convert X to Y by repeated
# division and multiplication
def solve(X, Y):
     
    # Check if X is greater than Y
    # then swap the elements
    if (X > Y):
        temp = X
        X = Y
        Y = temp
   
    # Check if X equals Y
    if (X == Y):
        print(0)
   
    elif (Y % X == 0):
        print(1)
    else:
        print(2)
  
# Driver code
X = 8
Y = 13
 
solve(X, Y)
 
# This code is contributed by code_hunt


C#
// C# implementation to find minimum
// steps to convert X to Y by repeated
// division and multiplication
using System;
 
class GFG{
 
static int solve(int X, int Y)
{
     
    // Check if X is greater than Y
    // then swap the elements
    if (X > Y)
    {
        int temp = X;
        X = Y;
        Y = temp;
    }
 
    // Check if X equals Y
    if (X == Y)
        Console.WriteLine(0);
 
    else if (Y % X == 0)
        Console.WriteLine(1);
    else
        Console.WriteLine(2);
    return 0;
}
 
// Driver code
public static void Main(String[] args)
{
    int X = 8, Y = 13;
    solve(X, Y);
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
2