给定一个整数N ,任务是通过涉及乘以2和除以10的最少操作次数将N减少到1 。如果无法获得1 ,则打印“-1” 。
例子:
Input: N = 5
Output: 2
Explanation:
Below are the operations performed:
1st operation: Multiply N by 2. Therefore, N = 5 * 2 = 10.
2nd operation: Divide N by 10. Therefore, N = 10/10 = 1.
Therefore, minimum number of operations required is 2.
Input: N = 4
Output: -1
方法:想法是检查给定数 M 的质因数。如果给定数具有2和5以外的质因数,则不可能通过给定运算将给定数减少到1 。如果2的素数数超过5 ,则不可能将N减少到1,因为2 的所有幂都不能减少。
请按照以下步骤解决问题:
- 计数2秒中存在的N个素数因子的数量并将其存储在一个变量,说CNT2和更新N到N / 2 CNT2。
- 计数5秒中存在的N个素数因子的数量并将其存储在一个变量,说CNT5和更新N到N / 5 CNT5。
- 完成上述步骤后,如果N为1且cnt2 ≤ 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)