📌  相关文章
📜  检查 X 是否可以通过在每个操作中转换为 3 * (X / 2) 或 X – 1 来转换为 Y

📅  最后修改于: 2021-10-25 10:33:51             🧑  作者: Mango

给定两个正整数XY ,任务是通过重复将X的值更改为(3 * X / 2)如果X是偶数)或(X – 1)来检查X 是否可以转换为Y。如果可以将X转换为Y ,则打印“是” 。否则,打印“否”

例子:

方法:根据以下观察可以解决给定的问题:

  • 如果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 ,则打印“是”
  • 如果值X1Y1 ,则打印“是”
  • 如果值X23并且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)