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

📅  最后修改于: 2021-04-17 14:20:45             🧑  作者: Mango

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

例子:

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

  • 如果X的值是至少Y,然后X总是可以利用第二操作转换为Y,即,由1减少X。
  • 如果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


输出:
Yes

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