📌  相关文章
📜  检查是否可以将A转换为B

📅  最后修改于: 2021-04-30 02:12:29             🧑  作者: Mango

给定两个整数AB。任务是检查通过多次执行以下操作,是否有可能将A转换为B。

  1. 将当前数字x转换为2 * x
  2. 将当前数字x转换为(10 * x)+1

例子:

方法:让我们以相反的方式解决此问题–尝试从B获得数字A。

请注意,如果B1结尾,则最后一个操作是将数字1附加到当前数字的右边。因此,我们删除B的最后一位并移至新数字。

如果最后一位是偶数,则最后一次操作是将当前数字乘以2 。因此,我们将B除以2并移至新数字。

在其他情况下(如果B以1以外的奇数结尾),答案为No。

每次获得新数字后,我们都需要重复描述的算法。如果在某个时候,我们得到一个等于A的数字,则答案为 ,如果新数字小于A,则答案为“否”

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if A can be
// converted to B with the given operations
bool canConvert(int a, int b)
{
    while (b > a) {
  
        // If the current number ends with 1
        if (b % 10 == 1) {
            b /= 10;
            continue;
        }
  
        // If the current number is divisible by 2
        if (b % 2 == 0) {
            b /= 2;
            continue;
        }
  
        // If above two conditions fail
        return false;
    }
  
    // If it is possible to convert A to B
    if (b == a)
        return true;
    return false;
}
  
// Driver code
int main()
{
    int A = 2, B = 82;
  
    if (canConvert(A, B))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
    // Function that returns true if A can be
    // converted to B with the given operations
    static boolean canConvert(int a, int b)
    {
        while (b > a) 
        {
  
            // If the current number ends with 1
            if (b % 10 == 1) 
            {
                b /= 10;
                continue;
            }
  
            // If the current number is divisible by 2
            if (b % 2 == 0)
            {
                b /= 2;
                continue;
            }
  
            // If above two conditions fail
            return false;
        }
  
        // If it is possible to convert A to B
        if (b == a)
            return true;
        return false;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
  
        int A = 2, B = 82;
  
        if (canConvert(A, B))
            System.out.println("Yes");
        else
            System.out.println("No");
  
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function that returns true if A can be 
# converted to B with the given operations 
def canConvert(a, b) : 
  
    while (b > a) :
  
        # If the current number ends with 1 
        if (b % 10 == 1) :
            b //= 10; 
            continue; 
          
        # If the current number is divisible by 2 
        if (b % 2 == 0) :
            b /= 2; 
            continue; 
  
        # If the above two conditions fail 
        return false; 
      
    # If it is possible to convert A to B 
    if (b == a) :
        return True;
          
    return False; 
  
# Driver code 
if __name__ == "__main__" : 
  
    A = 2; B = 82; 
  
    if (canConvert(A, B)) :
        print("Yes"); 
    else :
        print("No"); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
class GFG
{
  
    // Function that returns true if A can be
    // converted to B with the given operations
    static bool canConvert(int a, int b)
    {
        while (b > a) 
        {
  
            // If the current number ends with 1
            if (b % 10 == 1) 
            {
                b /= 10;
                continue;
            }
  
            // If the current number is divisible by 2
            if (b % 2 == 0)
            {
                b /= 2;
                continue;
            }
  
            // If above two conditions fail
            return false;
        }
  
        // If it is possible to convert A to B
        if (b == a)
            return true;
        return false;
    }
  
    // Driver code
    public static void Main() 
    {
  
        int A = 2, B = 82;
  
        if (canConvert(A, B))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
  
    }
}
  
// This code is contributed by anuj_67..


输出:
Yes