给定一个正整数N,任务是找到通过反复除以N乘其应有的除数或由1增大或减小n降低N到1所需的操作的最小数量。
例子:
Input: N = 9
Output: 3
Explanation:
The proper divisors of N(= 9) are {1, 3}. Following operations are performed to reduced N to 1:
Operation 1: Divide N(= 9) by 3(which is a proper divisor of N(= 9) modifies the value of N to 9/3 = 1.
Operation 2: Decrementing the value of N(= 3) by 1 modifies the value of N to 3 – 1 = 2.
Operation 3: Decrementing the value of N(= 2) by 1 modifies the value of N to 2 – 1 = 1.
Therefore, the total number of operations required is 3.
Input: N = 4
Output: 2
方法:根据以下观察可以解决给定的问题:
- 如果N的值是偶数,则可以通过将N除以N / 2然后将2减至1将其减小到值2 。因此,所需的最小步骤数为2 。
- 否则, N的值甚至可以通过递减它来实现,并且可以使用上述步骤将其减小到1 。
请按照以下步骤解决问题
- 初始化一个变量,比如cnt为0 ,以存储将N减少到1所需的最小步骤数。
- 迭代一个循环,直到N减少到1并执行以下步骤:
- 如果N的值等于2或 N 为奇数,则更新N = N – 1的值并将cnt增加1 。
- 否则,更新N = N / (N / 2) 的值并将cnt增加 1。
- 完成以上步骤后,打印cnt的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of steps required to reduce N to 1
int reduceToOne(long long int N)
{
// Stores the number
// of steps required
int cnt = 0;
while (N != 1) {
// If the value of N
// is equal to 2 or N is odd
if (N == 2 or (N % 2 == 1)) {
// Decrement N by 1
N = N - 1;
// Increment cnt by 1
cnt++;
}
// If N is even
else if (N % 2 == 0) {
// Update N
N = N / (N / 2);
// Increment cnt by 1
cnt++;
}
}
// Return the number
// of steps obtained
return cnt;
}
// Driver Code
int main()
{
long long int N = 35;
cout << reduceToOne(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the minimum number
// of steps required to reduce N to 1
static int reduceToOne(long N)
{
// Stores the number
// of steps required
int cnt = 0;
while (N != 1)
{
// If the value of N
// is equal to 2 or N is odd
if (N == 2 || (N % 2 == 1))
{
// Decrement N by 1
N = N - 1;
// Increment cnt by 1
cnt++;
}
// If N is even
else if (N % 2 == 0)
{
// Update N
N = N / (N / 2);
// Increment cnt by 1
cnt++;
}
}
// Return the number
// of steps obtained
return cnt;
}
// Driver Code
public static void main(String[] args)
{
long N = 35;
System.out.println(reduceToOne(N));
}
}
// This code is contributed by Dharanendra L V.
Python3
# python program for the above approach
# Function to find the minimum number
# of steps required to reduce N to 1
def reduceToOne(N):
# Stores the number
# of steps required
cnt = 0
while (N != 1):
# If the value of N
# is equal to 2 or N is odd
if (N == 2 or (N % 2 == 1)):
# Decrement N by 1
N = N - 1
# Increment cnt by 1
cnt += 1
# If N is even
elif (N % 2 == 0):
# Update N
N = N / (N / 2)
# Increment cnt by 1
cnt += 1
# Return the number
# of steps obtained
return cnt
# Driver Code
if __name__ == '__main__':
N = 35
print (reduceToOne(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
// of steps required to reduce N to 1
static int reduceToOne(long N)
{
// Stores the number
// of steps required
int cnt = 0;
while (N != 1)
{
// If the value of N
// is equal to 2 or N is odd
if (N == 2 || (N % 2 == 1))
{
// Decrement N by 1
N = N - 1;
// Increment cnt by 1
cnt++;
}
// If N is even
else if (N % 2 == 0)
{
// Update N
N = N / (N / 2);
// Increment cnt by 1
cnt++;
}
}
// Return the number
// of steps obtained
return cnt;
}
// Driver Code
public static void Main()
{
long N = 35;
Console.WriteLine(reduceToOne(N));
}
}
// This code s contributed by code_hunt.
Javascript
输出:
3
时间复杂度: O(1)
辅助空间: O(1)