📌  相关文章
📜  通过将每一步乘以 2、3、4 或 5,从 1 达到 N 的最小步数

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

给定一个整数N ,任务是通过将每一步乘以 2、3、4 或 5 来找到从 1 达到数字N的最少步数。如果不可能达到 N,则打印 -1。
例子:

方法:思想是用贪心算法来选择每一步应该执行的操作,并以相反的方式执行操作,而不是从1到N,找到达到N到1所需的操作。下面是步骤图示:

  • 应用以下操作,直到 N 大于 1。
  • 检查 N 是否可被 5 整除,然后将步长增加 1 并将 N 减少到 N/5
  • 否则,检查 N 是否可被 4 整除,然后将步长增加 1 并将 N 减少到 N/4
  • 否则,检查 N 是否可被 3 整除,然后将步长增加 1 并将 N 减少到 N/3
  • 否则,检查 N 是否可被 2 整除,然后将步长增加 1,并将 N 减少到 N/2
  • 如果在任何步骤都无法应用操作,则不可能有一组操作可以从 1 到达 N。因此,返回 -1。

下面是上述方法的实现:

C++
// C++ implementation to find
// minimum number of steps
// to reach N from 1
 
#include 
 
using namespace std;
 
// Function to find a minimum number
// of steps to reach N from 1
int Minsteps(int n)
{
    int ans = 0;
 
    // Check until N is greater
    // than 1 and operations
    // can be applied
    while (n > 1) {
 
        // Condition to choose the
        // operations greedily
        if (n % 5 == 0) {
 
            ans++;
            n = n / 5;
            continue;
        }
        else if (n % 4 == 0) {
            ans++;
            n = n / 4;
            continue;
        }
        else if (n % 3 == 0) {
            ans++;
            n = n / 3;
            continue;
        }
        else if (n % 2 == 0) {
            ans++;
            n = n / 2;
            continue;
        }
        return -1;
    }
    return ans;
}
 
// Driver code
int main()
{
    int n = 10;
    cout << Minsteps(n);
    return 0;
}


Java
// Java implementation to find
// minimum number of steps
// to reach N from 1
 
import java.util.*;
 
class GFG{
 
// Function to find a minimum number
// of steps to reach N from 1
static int Minsteps(int n)
{
    int ans = 0;
 
    // Check until N is greater
    // than 1 and operations
    // can be applied
    while (n > 1)
    {
         
        // Condition to choose the
        // operations greedily
        if (n % 5 == 0)
        {
            ans++;
            n = n / 5;
            continue;
        }
        else if (n % 4 == 0)
        {
            ans++;
            n = n / 4;
            continue;
        }
        else if (n % 3 == 0)
        {
            ans++;
            n = n / 3;
            continue;
        }
        else if (n % 2 == 0)
        {
            ans++;
            n = n / 2;
            continue;
        }
        return -1;
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    System.out.print(Minsteps(n));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 implementation to find
# minimum number of steps
# to reach N from 1
 
# Function to find a minimum number
# of steps to reach N from 1
def Minsteps(n):
 
    ans = 0
 
    # Check until N is greater
    # than 1 and operations
    # can be applied
    while (n > 1):
 
        # Condition to choose the
        # operations greedily
        if (n % 5 == 0):
            ans = ans + 1
            n = n / 5
            continue
 
        elif (n % 4 == 0):
            ans = ans + 1
            n = n / 4
            continue
 
        elif (n % 3 == 0):
            ans = ans + 1
            n = n / 3
            continue
 
        elif (n % 2 == 0):
            ans = ans + 1
            n = n / 2
            continue
 
        return -1
 
    return ans
 
# Driver code
n = 10
print(Minsteps(n))
 
# This code is contributed by Pratik


C#
// C# implementation to find
// minimum number of steps
// to reach N from 1
using System;
 
class GFG{
 
// Function to find a minimum number
// of steps to reach N from 1
static int Minsteps(int n)
{
    int ans = 0;
 
    // Check until N is greater
    // than 1 and operations
    // can be applied
    while (n > 1)
    {
         
        // Condition to choose the
        // operations greedily
        if (n % 5 == 0)
        {
            ans++;
            n = n / 5;
            continue;
        }
        else if (n % 4 == 0)
        {
            ans++;
            n = n / 4;
            continue;
        }
        else if (n % 3 == 0)
        {
            ans++;
            n = n / 3;
            continue;
        }
        else if (n % 2 == 0)
        {
            ans++;
            n = n / 2;
            continue;
        }
        return -1;
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int n = 10;
    Console.Write(Minsteps(n));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
2