📌  相关文章
📜  通过将N乘A并将M减B来检查N和M是否可以相等

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

给定四个数字MNAB ,任务是通过执行以下任一操作来检查MN是否可以彼此相等:

  • M可以增加AN可以减少B
  • 保持原样。

例子:

方法:仔细观察,可以发现由于我们增加M而减少N,因此只有当M小于N时,它们才能相等。因此,当M小于N时,每一步都有两种情况:

  1. M可以增加A,N可以减少B。
  2. 保持原样。

可以得出的另一个观察结果是,当M增加而N减小时, MN之间的绝对距离减小A + B倍。例如:

从以上示例可以得出结论,只有通过检查MN之间的绝对距离是否为(A + B)的倍数,才能在恒定时间内解决此问题。

  • 如果是倍数,则M和N可以相等。
  • 否则,它们不能相等。

下面是上述方法的实现:

C++
// C++ program to check if two numbers
// can be made equal by increasing
// the first by a and decreasing
// the second by b
  
#include 
using namespace std;
  
// Function to whether the numbers
// can be made equal or not
bool checkEqualNo(int m, int n, int a, int b)
{
    if (m <= n) {
  
        // Check whether the numbers can reach
        // an equal point or not
        if ((n - m) % (a + b) == 0) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
  
        // M and N cannot be made equal by
        // increasing M and decreasing N when
        // M is already greater than N
        return false;
    }
}
  
// Driver code
int main()
{
    int M = 2, N = 8;
    int A = 3, B = 3;
  
    if (checkEqualNo(M, N, A, B)) 
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
  
return 0;
}


Java
// Java program to check if two numbers
// can be made equal by increasing
// the first by a and decreasing
// the second by b
class GFG 
{
      
    // Function to whether the numbers
    // can be made equal or not
    static boolean checkEqualNo(int m, int n, int a, int b)
    {
        if (m <= n) {
      
            // Check whether the numbers can reach
            // an equal point or not
            if ((n - m) % (a + b) == 0) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
      
            // M and N cannot be made equal by
            // increasing M and decreasing N when
            // M is already greater than N
            return false;
        }
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int M = 2, N = 8;
        int A = 3, B = 3;
      
        if (checkEqualNo(M, N, A, B) == true) 
            System.out.println("Yes");
        else
            System.out.println("No");
      
    }
}
  
// This code is contributed by Yash_R


Python3
# Python3 program to check if two numbers
# can be made equal by increasing
# the first by a and decreasing
# the second by b
  
# Function to whether the numbers
# can be made equal or not
def checkEqualNo(m, n, a, b) :
    if (m <= n) :
  
        # Check whether the numbers can reach
        # an equal point or not
        if ((n - m) % (a + b) == 0) :
            return True;
      
        else :
            return False;
          
    else :
  
        # M and N cannot be made equal by
        # increasing M and decreasing N when
        # M is already greater than N
        return False;
      
# Driver code
if __name__ == "__main__" :
  
    M = 2; N = 8;
    A = 3; B = 3;
  
    if (checkEqualNo(M, N, A, B)) :
        print("Yes");
    else :
        print("No");
  
# This code is contributed by Yash_R


C#
// C# program to check if two numbers
// can be made equal by increasing
// the first by a and decreasing
// the second by b
using System;
  
class GFG 
{
      
    // Function to whether the numbers
    // can be made equal or not
    static bool checkEqualNo(int m, int n, int a, int b)
    {
        if (m <= n) {
      
            // Check whether the numbers can reach
            // an equal point or not
            if ((n - m) % (a + b) == 0) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
      
            // M and N cannot be made equal by
            // increasing M and decreasing N when
            // M is already greater than N
            return false;
        }
    }
      
    // Driver code
    public static void Main (String[] args)
    {
        int M = 2;
        int N = 8;
        int A = 3;
        int B = 3;
      
        if (checkEqualNo(M, N, A, B) == true) 
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by Yash_R


输出:
Yes