给定两个正整数X和Y ,任务是检查是否可以通过将X乘以2或在X的末尾附加1将数字X转换为Y 。如果可以将X转换为Y ,则打印“是” 。否则,打印“否” 。
例子:
Input: X = 100, Y = 40021
Output: Yes
Explanation:
Below are the operations performed to convert X into Y:
Operation 1: Multiply X(= 100) by 2, modifies the value X to 200.
Operation 2: Append 1 at the end of X(= 200), modifies the value X to 2001.
Operation 3: Multiply X(= 2001) by 2, modifies the value X to 4002.
Operation 4: Append 1 at the end of X(= 4002), modifies the value X to 40021.
Therefore, from the above operations, it can be seen that the value X can be converted into Y. Hence, print Yes.
Input: X = 17 and Y = 35
Output: No
方法:给定的问题可以通过以相反的方式执行操作来解决,即尝试将值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)