📌  相关文章
📜  通过重复将 X 乘以 2 或在末尾附加 1 将 X 转换为 Y

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

给定两个正整数XY ,任务是检查是否可以通过将X乘以2或在X的末尾附加1将数字X转换为Y 。如果可以将X转换为Y ,则打印“是” 。否则,打印“否”

例子:

方法:给定的问题可以通过以相反的方式执行操作来解决,即尝试将值Y转换为X 。请按照以下步骤解决问题:

  • 迭代直到Y的值大于X并执行以下步骤:
    • 如果Y的最后一位的值为 1,则将Y的值除以10
    • 否则,如果Y的值可被 2 整除,则将Y除以2
    • 否则,跳出循环。
  • 完成上述步骤后,如果Y的值与X的值相同,则打印Yes 。否则,打印No

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Function to check if X can be
// converted to Y by multiplying
// X by 2 or appending 1 at the end
void convertXintoY(int X, int Y)
{
    // Iterate until Y is at least X
    while (Y > X) {
  
        // If Y is even
        if (Y % 2 == 0)
            Y /= 2;
  
        // If the last digit of Y is 1
        else if (Y % 10 == 1)
            Y /= 10;
  
        // Otherwise
        else
            break;
    }
  
    // Check if X is equal to Y
    if (X == Y)
        cout << "Yes";
    else
        cout << "No";
}
  
// Driver Code
int main()
{
    int X = 100, Y = 40021;
    convertXintoY(X, Y);
  
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function to check if X can be
// converted to Y by multiplying
// X by 2 or appending 1 at the end
static void convertXintoY(int X, int Y)
{
    // Iterate until Y is at least X
    while (Y > X) {
  
        // If Y is even
        if (Y % 2 == 0)
            Y /= 2;
  
        // If the last digit of Y is 1
        else if (Y % 10 == 1)
            Y /= 10;
  
        // Otherwise
        else
            break;
    }
  
    // Check if X is equal to Y
    if (X == Y)
        System.out.print("Yes");
    else
        System.out.print("No");
}
  
// Driver Code
public static void main(String[] args)
{
    int X = 100, Y = 40021;
    convertXintoY(X, Y);
}
}
  
// This code is contributed by sanjoy_62.


Python3
# Python program for the above approach
  
# Function to check if X can be
# converted to Y by multiplying
# X by 2 or appending 1 at the end
def convertXintoY(X, Y):
    # Iterate until Y is at least X
    while (Y > X):
  
        # If Y is even
        if (Y % 2 == 0):
            Y //= 2
  
        # If the last digit of Y is 1
        elif (Y % 10 == 1):
            Y //= 10
  
        # Otherwise
        else:
            break
  
    # Check if X is equal to Y
    if (X == Y):
        print("Yes")
    else:
        print("No")
  
# Driver Code
if __name__ == '__main__':
    X,Y = 100, 40021
    convertXintoY(X, Y)
  
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to check if X can be
// converted to Y by multiplying
// X by 2 or appending 1 at the end
static void convertXintoY(int X, int Y)
{
      
    // Iterate until Y is at least X
    while (Y > X) 
    {
          
        // If Y is even
        if (Y % 2 == 0)
            Y /= 2;
  
        // If the last digit of Y is 1
        else if (Y % 10 == 1)
            Y /= 10;
  
        // Otherwise
        else
            break;
    }
  
    // Check if X is equal to Y
    if (X == Y)
        Console.Write("Yes");
    else
        Console.Write("No");
}
  
// Driver Code
public static void Main(String[] args)
{
    int X = 100, Y = 40021;
      
    convertXintoY(X, Y);
}
}
  
// This code is contributed by shivanisinghss2110


Javascript


输出:
Yes

时间复杂度: log(Y)
辅助空间: O(1)