给定两个正整数X和Y ,任务是通过重复将X的值更改为(3 * X / 2) (如果X是偶数)或(X – 1)来检查X 是否可以转换为Y。如果可以将X转换为Y ,则打印“是” 。否则,打印“否” 。
例子:
Input: X = 6, Y = 8
Output: Yes
Explanation:
Operation 1: Convert X(= 6) to 3*X/2 ( = 3 * (6 / 2) = 9).
Operation 2: Convert X(= 9) to (X – 1) (= 8).
Therefore, the total number of operations required is 2.
Input: X = 3, Y = 6
Output: No
方法:根据以下观察可以解决给定的问题:
- 如果X的值至少是Y ,那么X总是可以使用第二个操作转换为Y ,即,将X减少1 。
- 如果X是偶数,那么它可以转换为(3 * (X / 2)) ,对于大于0 的所有偶数,它都大于X 。
- 如果X的值是奇数,那么X可以转换为(X – 1) ,可以转换为(3 * (X – 1)/2) ,它大于X 。
- 因此,从上述观察,X转化成Y是总是可能的X> 3。
- 需要考虑以下基本情况:
- X = 1:仅当Y = 1时才可能进行转换。
- X = 2 或 X = 3:仅当Y ≤ 3时才可能进行转换。
- 在所有其他情况下,转换是不可能的。
请按照以下步骤解决问题:
- 如果值X大于4 ,则打印“是” 。
- 如果值X为1且Y为1 ,则打印“是” 。
- 如果值X是2或3并且Y小于4 ,则打印“是” 。
- 否则,为所有其他情况打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if X can be
// made equal to Y by converting
// X to (3*X/2) or (X - 1)
void check(int X, int Y)
{
// Conditions for possible conversion
if (X > 3) {
cout << "Yes";
}
else if (X == 1 and Y == 1) {
cout << "Yes";
}
else if (X == 2 and Y <= 3) {
cout << "Yes";
}
else if (X == 3 and Y <= 3) {
cout << "Yes";
}
// Otherwise, conversion
// is not possible
else {
cout << "No";
}
}
// Driver Code
int main()
{
int X = 6, Y = 8;
check(X, Y);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if X can be
// made equal to Y by converting
// X to (3*X/2) or (X - 1)
static void check(int X, int Y)
{
// Conditions for possible conversion
if (X > 3)
{
System.out.print("Yes");
}
else if (X == 1 && Y == 1)
{
System.out.print("Yes");
}
else if (X == 2 && Y <= 3)
{
System.out.print("Yes");
}
else if (X == 3 && Y <= 3)
{
System.out.print("Yes");
}
// Otherwise, conversion
// is not possible
else
{
System.out.print("No");
}
}
// Driver Code
public static void main (String[] args)
{
int X = 6, Y = 8;
check(X, Y);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to check if X can be
# made equal to Y by converting
# X to (3*X/2) or (X - 1)
def check(X, Y):
# Conditions for possible conversion
if (X > 3):
print("Yes")
elif (X == 1 and Y == 1):
print("Yes")
elif (X == 2 and Y <= 3):
print("Yes")
elif (X == 3 and Y <= 3):
print("Yes")
# Otherwise, conversion
# is not possible
else:
print("No")
# Driver Code
if __name__ == '__main__':
X = 6
Y = 8
check(X, Y)
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if X can be
// made equal to Y by converting
// X to (3*X/2) or (X - 1)
static void check(int X, int Y)
{
// Conditions for possible conversion
if (X > 3)
{
Console.WriteLine("Yes");
}
else if (X == 1 && Y == 1)
{
Console.WriteLine("Yes");
}
else if (X == 2 && Y <= 3)
{
Console.WriteLine("Yes");
}
else if (X == 3 && Y <= 3)
{
Console.WriteLine("Yes");
}
// Otherwise, conversion
// is not possible
else
{
Console.WriteLine("No");
}
}
// Driver Code
public static void Main(string[] args)
{
int X = 6, Y = 8;
check(X, Y);
}
}
// This code is contributed by avijitmondal1998
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)