📜  将给定数字减少到 1 所需的最少除以 10 和乘以 2

📅  最后修改于: 2021-10-26 02:36:06             🧑  作者: Mango

给定一个整数N ,任务是通过涉及乘以2和除以10的最少操作次数将N减少到1 。如果无法获得1 ,则打印“-1”

例子:

方法:想法是检查给定数 M 的质因数。如果给定数具有25以外的质因数,则不可能通过给定运算将给定数减少到1 。如果2的素数数超过5 ,则不可能N减少到1,因为2 的所有幂都不能减少。
请按照以下步骤解决问题:

  • 计数2秒中存在的N个素数因子的数量并将其存储在一个变量,说CNT2和更新NN / 2 CNT2。
  • 计数5秒中存在的N个素数因子的数量并将其存储在一个变量,说CNT5和更新NN / 5 CNT5。
  • 完成上述步骤后,如果N1cnt2 ≤ cnt5 ,则所需的最小步骤数为2 * cnt5 – cnt2
  • 否则,打印“-1”,因为N不能用给定的操作减少到1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
// operations required to reduce N to 1
int minimumMoves(int n)
{
 
    // Stores count of powers of 2 and 5
    int cnt2 = 0, cnt5 = 0;
 
    // Calculating the primefactors 2
    while (n % 2 == 0) {
        n /= 2;
        cnt2++;
    }
 
    // Calculating the primefactors 5
    while (n % 5 == 0) {
        n /= 5;
        cnt5++;
    }
 
    // If n is 1 and cnt2 <= cnt5
    if (n == 1 && cnt2 <= cnt5) {
 
        // Return the minimum operations
        return 2 * cnt5 - cnt2;
    }
 
    // Otherwise, n can't be reduced
    else
        return -1;
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 25;
 
    // Function Call
    cout << minimumMoves(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum number
// operations required to reduce N to 1
static int minimumMoves(int n)
{
     
    // Stores count of powers of 2 and 5
    int cnt2 = 0, cnt5 = 0;
 
    // Calculating the primefactors 2
    while (n % 2 == 0)
    {
        n /= 2;
        cnt2++;
    }
 
    // Calculating the primefactors 5
    while (n % 5 == 0)
    {
        n /= 5;
        cnt5++;
    }
 
    // If n is 1 and cnt2 <= cnt5
    if (n == 1 && cnt2 <= cnt5)
    {
         
        // Return the minimum operations
        return 2 * cnt5 - cnt2;
    }
 
    // Otherwise, n can't be reduced
    else
        return -1;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Number N
    int N = 25;
 
    // Function Call
    System.out.print(minimumMoves(N));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# operations required to reduce N to 1
def minimumMoves(n):
 
    # Stores count of powers of 2 and 5
    cnt2 = 0
    cnt5 = 0
 
    # Calculating the primefactors 2
    while (n % 2 == 0):
        n //= 2
        cnt2 += 1
 
    # Calculating the primefactors 5
    while (n % 5 == 0):
        n //= 5
        cnt5 += 1
 
    # If n is 1 and cnt2 <= cnt5
    if (n == 1 and cnt2 <= cnt5):
         
        # Return the minimum operations
        return 2 * cnt5 - cnt2
 
    # Otherwise, n can't be reduced
    else:
        return -1
 
# Driver Code
if __name__ == '__main__':
     
    # Given Number N
    N = 25
 
    # Function Call
    print(minimumMoves(N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum number
// operations required to reduce N to 1
static int minimumMoves(int n)
{
     
    // Stores count of powers of 2 and 5
    int cnt2 = 0, cnt5 = 0;
 
    // Calculating the primefactors 2
    while (n % 2 == 0)
    {
        n /= 2;
        cnt2++;
    }
 
    // Calculating the primefactors 5
    while (n % 5 == 0)
    {
        n /= 5;
        cnt5++;
    }
 
    // If n is 1 and cnt2 <= cnt5
    if (n == 1 && cnt2 <= cnt5)
    {
         
        // Return the minimum operations
        return 2 * cnt5 - cnt2;
    }
 
    // Otherwise, n can't be reduced
    else
        return -1;
}
 
// Driver Code
public static void Main()
{
     
    // Given Number N
    int N = 25;
 
    // Function Call
    Console.WriteLine(minimumMoves(N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
4

时间复杂度: O(log N)
辅助空间: O(1)