📌  相关文章
📜  通过多次使用给定的运算来检查X和Y是否可以设为零

📅  最后修改于: 2021-04-24 17:22:10             🧑  作者: Mango

给定两个整数XY ,任务是检查是否可以通过使用给定的操作多次来使这两个整数等于0。操作描述如下-

  • 选择任意整数Z。
  • 使用以下任一值更新值:
    • X = X – 2 * Z,Y = Y – 3 * Z
    • X = X – 3 * Z,Y = Y – 2 * Z

例子:

方法:假设X≤Y。如果满足以下两个条件,则答案为“是”:

  • (X + Y)mod 5 = 0 :因为在每次操作后,(X + Y)mod 5的值不变。
    假设已经选择了任意数字Z。
    因此,值(X + Y)将更改为
    ((X - 3Z) + (Y - 2Z))

    这等于

    (X + Y - 5Z)

    为了使该值等于0, X + Y = 5Z 。因此,在两侧取mod时,(X + Y)mod 5必须等于0。

  • 3 * X> = 2 * Y,因此减法不会使X和Y的值变为负数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
  
#include 
using namespace std;
  
// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
void ifPossible(int X, int Y)
{
    if (X > Y)
        swap(X, Y);
  
    // Check for the two conditions
    if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y)
        cout << "Yes";
    else
        cout << "No";
}
  
// Driver code
int main()
{
    int X = 33, Y = 27;
  
    ifPossible(X, Y);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
static void ifPossible(int X, int Y)
{
    if (X > Y)
        swap(X, Y);
  
    // Check for the two conditions
    if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
        System.out.print("Yes");
    else
        System.out.print("No");
}
static void swap(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}
  
// Driver code
public static void main(String[] args)
{
    int X = 33, Y = 27;
  
    ifPossible(X, Y);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function to check if X and Y
# can be made equal to zero by
# using given operation any number of times
def ifPossible(X, Y):
    if (X > Y):
        X, Y = Y, X
  
    # Check for the two conditions
    if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y):
        print("Yes")
    else:
        print("No")
  
# Driver code
X = 33
Y = 27
  
ifPossible(X, Y)
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
static void ifPossible(int X, int Y)
{
    if (X > Y)
        swap(X, Y);
  
    // Check for the two conditions
    if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
static void swap(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}
  
// Driver code
public static void Main()
{
    int X = 33, Y = 27;
  
    ifPossible(X, Y);
}
}
  
// This code is contributed by Yash_R


输出:
Yes