📌  相关文章
📜  将 N 减少到 1 所需的适当除数的最小减量或除法

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

给定一个正整数N,任务是找到通过反复除以N乘其应有的除数或由1增大或减小n降低N1所需的操作的最小数量。

例子:

方法:根据以下观察可以解决给定的问题:

  • 如果N的值是偶数,则可以通过将N除以N / 2然后将2减至1将其减小到值2 。因此,所需的最小步骤数为2
  • 否则, N的值甚至可以通过递减它来实现,并且可以使用上述步骤将其减小到1

请按照以下步骤解决问题

  • 初始化一个变量,比如cnt0 ,以存储将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)