📌  相关文章
📜  将n转换为m所需的给定运算的最小数目

📅  最后修改于: 2021-04-24 18:34:40             🧑  作者: Mango

给定两个整数nm ,在一次运算中, n可以乘以23 。任务是用最少的给定操作数将n转换为m 。如果无法通过给定的操作将n转换为m ,则打印-1

例子:

方法:如果m不能被n整除,则打印-1,因为无法通过给定的操作将n转换为m 。另外,我们可以检查除法时商是否只有23作为主要因子。如果是,那么结果将是23的幂的和,否则打印-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum
// operations required
int minOperations(int n, int m)
{
    if (m % n != 0)
        return -1;
  
    int minOperations = 0;
    int q = m / n;
  
    // Counting all 2s
    while (q % 2 == 0) {
        q = q / 2;
        minOperations++;
    }
  
    // Counting all 3s
    while (q % 3 == 0) {
        q = q / 3;
        minOperations++;
    }
  
    // If q contained only 2 and 3
    // as the only prime factors
    // then it must be 1 now
    if (q == 1)
        return minOperations;
  
    return -1;
}
  
// Driver code
int main()
{
    int n = 120, m = 51840;
    cout << minOperations(n, m);
  
    return 0;
}


Java
// Java implementation of the approach
class GfG {
  
    // Function to return the minimum
    // operations required
    static int minOperations(int n, int m)
    {
        if (m % n != 0)
            return -1;
  
        int minOperations = 0;
        int q = m / n;
  
        // Counting all 2s
        while (q % 2 == 0) {
            q = q / 2;
            minOperations++;
        }
  
        // Counting all 3s
        while (q % 3 == 0) {
            q = q / 3;
            minOperations++;
        }
  
        // If q contained only 2 and 3
        // as the only prime factors
        // then it must be 1 now
        if (q == 1)
            return minOperations;
  
        return -1;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 120, m = 51840;
        System.out.println(minOperations(n, m));
    }
}


Python3
# Python 3 implementation of the approach
  
# Function to return the minimum
# operations required
def minOperations(n, m):
    if (m % n != 0):
        return -1
  
    minOperations = 0
    q = int(m / n)
  
    # Counting all 2s
    while (q % 2 == 0):
        q = int(q / 2)
        minOperations += 1
  
    # Counting all 3s
    while (q % 3 == 0):
        q = int(q / 3)
        minOperations += 1
  
    # If q contained only 2 and 3
    # as the only prime factors
    # then it must be 1 now
    if (q == 1):
        return minOperations
  
    return -1
  
# Driver code
if __name__ == '__main__':
    n = 120
    m = 51840
    print(minOperations(n, m))
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to return the minimum
// operations required
static int minOperations(int n, int m)
{
    if (m % n != 0)
        return -1;
  
    int minOperations = 0;
    int q = m / n;
  
    // Counting all 2s
    while (q % 2 == 0)
    {
        q = q / 2;
        minOperations++;
    }
  
    // Counting all 3s
    while (q % 3 == 0)
    {
        q = q / 3;
        minOperations++;
    }
  
    // If q contained only 2 and 3
    // as the only prime factors
    // then it must be 1 now
    if (q == 1)
        return minOperations;
  
    return -1;
}
  
// Driver code
public static void Main()
{
    int n = 120, m = 51840;
    Console.WriteLine(minOperations(n, m));
}
}
  
// This code is contributed 
// by Akanksha Rai


PHP


输出:
7