📌  相关文章
📜  从N找到达到M的最小步骤数

📅  最后修改于: 2021-05-07 08:56:18             🧑  作者: Mango

给定两个整数NM。任务是通过执行给定的操作找到从N到M的最小步数。

  1. 将数字x乘以2。因此,x变成2 * x。
  2. 从数字x中减去1。因此,x变为x-1。

例子:

Input : N = 4, M = 6
Output : 2
Explanation : Perform operation number 2 on N. 
So, N becomes 3 and then perform operation number 1. 
Then, N becomes 6. So, the minimum number of steps is 2. 

Input : N = 10, M = 1
Output : 9
Explanation : Perform operation number two 
9 times on N. Then N becomes 1.

方法
想法是按如下方式解决该问题:我们应该使用以下运算从M开始获得数字N:

  1. 如果是偶数,则将其除以2。
  2. 将数字加1。

现在,最小操作数为:

  1. 如果N> M,则返回它们之间的差,即,步数将对M加1,直到等于N。
  2. 否则,如果N
  3. 继续将M除以2,直到它变得小于N。如果M为奇数,则先向其加1,然后除以2。一旦M小于N,则将它们之间的差与上述操作的计数相加。 。

下面是上述方法的实现:

C++
// CPP program to find minimum number
// of steps to reach M from N
#include 
using namespace std;
 
// Function to find a minimum number
// of steps to reach M from N
int Minsteps(int n, int m)
{
    int ans = 0;
     
    // Continue till m is greater than n
    while(m > n)
    {
        // If m is odd
        if(m&1)
        {
            // add one
            m++;
            ans++;
        }
         
        // divide m by 2       
        m /= 2;
        ans++;
    }
     
    // Return the required answer
    return ans + n - m;
}
 
// Driver code
int main()
{
    int n = 4, m = 6;
    
    cout << Minsteps(n, m);
     
    return 0;
}


Java
// Java program to find minimum number
// of steps to reach M from N
class CFG
{
     
// Function to find a minimum number
// of steps to reach M from N
static int Minsteps(int n, int m)
{
    int ans = 0;
     
    // Continue till m is greater than n
    while(m > n)
    {
        // If m is odd
        if(m % 2 != 0)
        {
            // add one
            m++;
            ans++;
        }
         
        // divide m by 2    
        m /= 2;
        ans++;
    }
     
    // Return the required answer
    return ans + n - m;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 4, m = 6;
     
    System.out.println(Minsteps(n, m));
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 program to find minimum number
# of steps to reach M from N
 
# Function to find a minimum number
# of steps to reach M from N
def Minsteps(n, m):
 
    ans = 0
     
    # Continue till m is greater than n
    while(m > n):
 
        # If m is odd
        if(m & 1):
             
            # add one
            m += 1
            ans += 1
         
        # divide m by 2    
        m //= 2
        ans += 1
     
    # Return the required answer
    return ans + n - m
 
# Driver code
n = 4
m = 6
 
print(Minsteps(n, m))
 
# This code is contributed by mohit kumar


C#
// C# program to find minimum number
// of steps to reach M from N
using System;
 
class GFG
{
     
// Function to find a minimum number
// of steps to reach M from N
static int Minsteps(int n, int m)
{
    int ans = 0;
     
    // Continue till m is greater than n
    while(m > n)
    {
        // If m is odd
        if(m % 2 != 0)
        {
            // add one
            m++;
            ans++;
        }
         
        // divide m by 2    
        m /= 2;
        ans++;
    }
     
    // Return the required answer
    return ans + n - m;
}
 
// Driver code
public static void Main()
{
    int n = 4, m = 6;
     
    Console.WriteLine(Minsteps(n, m));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP
 $n)
    {
        // If m is odd
        if($m % 2 != 0)
        {
            // add one
            $m++;
            $ans++;
        }
         
        // divide m by 2    
        $m /= 2;
        $ans++;
    }
     
    // Return the required answer
    return $ans + $n - $m;
}
 
// Driver code
$n = 4; $m = 6;
 
echo(Minsteps($n, $m));
 
// This code is contributed by Code_Mech
?>


Javascript


输出:
2