给定整数N ,任务是通过将每个步数乘以2、3、4或5来找到从1达到N的最小步数。如果不可能达到N,则打印-1。
例子:
Input: N = 10
Output: 2
Explanation:
Initial number = 1
Step 1: Multiply it by 2, Current Number = 2
Step 2: Multiply it by 5, Current Number = 10
Therefore, Minimum 2 steps required to reach 10.
Input: N = 13
Output: -1
Explanation:
There is no way reach 13 using any given operations
方法:想法是使用Greedy Alogorithm选择应该在每个步骤执行的操作,并以相反的方式执行操作,而不是从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