给定一个整数N,找出将N更改为1的最少操作次数。如果不可能,则打印-1。一种操作定义为将N转换为数字2*N或将N转换为数字N/10(仅当 N 可被 10 整除时)。
例子:
Input: N = 50
Output: 3
Explanation: N can be converted to 1 in 3 steps as follows:
-> Firstly, multiply N by 2 and change N from 50 to 100 (2*N)
-> Then, divide N by 10 and change N from 100 to 10 (N/10)
-> And then, divide N by 10 and change N from 10 to 1 (N/10)
Input: N = 120
Output: -1
Explanation: There is no possible way for Geek to get to position 1.
方法:仅当N可以通过在每一步乘以2或除以10减少到1 时, N才能转换为1 。以下步骤如果N具有比图2和5等的一个主要因素,现在n不能降低到1。此外,如果在N的质因数分解中2s的数量超过5s ,则N不能将其减少到1,因为不可能将所有2s中和。 2s和5s 的相等数可以通过将数字除以10来中和。额外的5s可以通过乘以额外的2s然后除以10来抵消。请按照以下步骤解决问题:
- 将变量twos和Fives初始化为0,以计算数字N的质因数分解中2的数量和5的数量。
- 在 while 循环中迭代直到N%2等于0并执行以下步骤:
- 将数字N除以2。
- 将twos的值增加1。
- 在 while 循环中迭代直到N%5等于0并执行以下步骤:
- 将数字N除以5。
- 增加1五岁以下儿童的值。
- 如果N等于1和二进制补码的数目小于等于击掌的数量,然后,打印2 *击掌两岁作为答案。
- 否则,打印-1。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N can be changed
// to 1 or not.
void check(int N)
{
int twos = 0, fives = 0;
// Count the number of 2 in the prime
// factorisation of N
while (N % 2 == 0) {
N /= 2;
twos++;
}
// Count the number of 5 in the prime
// factorisation of N
while (N % 5 == 0) {
N /= 5;
fives++;
}
if (N == 1 && twos <= fives) {
cout << 2 * fives - twos;
}
else {
cout << -1;
}
}
// Driver Code
int main()
{
int N = 50;
check(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to check if N can be changed
// to 1 or not.
static void check(int N)
{
int twos = 0, fives = 0;
// Count the number of 2 in the prime
// factorisation of N
while (N % 2 == 0)
{
N /= 2;
twos++;
}
// Count the number of 5 in the prime
// factorisation of N
while (N % 5 == 0)
{
N /= 5;
fives++;
}
if (N == 1 && twos <= fives)
{
System.out.println( 2 * fives - twos);
}
else
{
System.out.println(-1);
}
}
// Driver Code
public static void main (String[] args)
{
int N = 50;
check(N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 program for the above approach
# Function to check if N can be changed
# to 1 or not.
def check(N):
twos = 0
fives = 0
# Count the number of 2 in the prime
# factorisation of N
while (N % 2 == 0):
N /= 2
twos += 1
# Count the number of 5 in the prime
# factorisation of N
while (N % 5 == 0):
N /= 5
fives += 1
if (N == 1 and twos <= fives):
print(2 * fives - twos)
else:
print(-1)
# Driver Code
if __name__ == '__main__':
N = 50
check(N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if N can be changed
// to 1 or not.
static void check(int N)
{
int twos = 0, fives = 0;
// Count the number of 2 in the prime
// factorisation of N
while (N % 2 == 0) {
N /= 2;
twos++;
}
// Count the number of 5 in the prime
// factorisation of N
while (N % 5 == 0) {
N /= 5;
fives++;
}
if (N == 1 && twos <= fives) {
Console.Write( 2 * fives - twos);
}
else {
Console.Write(-1);
}
}
// Driver Code
public static void Main()
{
int N = 50;
check(N);
}
}
// This code is contributed by sanjoy_62.
Javascript
3
时间复杂度: O(log N)
辅助空间: O(1)