给定两个整数A和B.任务是检查,如果A和B是相同或不不使用运算符。
例子:
Input : A = 5 , B = 6
Output : 0
Input : A = 5 , B = 5
Output : 1
Note : 1 = "YES" and 0 = "NO"
这个想法很简单,我们对两个元素(A,B)进行Xor操作。如果Xor为零,则两个数相等,否则不相等。
下面是上述想法的实现:
C++
// C++ program to compare two integers witout
// any comparison operator.
#include
using namespace std;
// function return true if A ^ B > 0 else false
bool EqualNumber(int A, int B)
{
return ( A ^ B ) ;
}
// Driver program
int main()
{
int A = 5 , B = 6;
cout << !EqualNumber(A, B) << endl;
return 0;
}
Java
// Java program to compare two integers witout
// any comparison operator.
import java.util.*;
class solution
{
// function return true if A ^ B > 0 else false
static boolean EqualNumber(int A, int B)
{
if ((A^B) != 0)
return true;
else
return false;
}
// Driver program
public static void main(String args[])
{
int A = 5 , B = 6;
if(EqualNumber(A, B) == false)
System.out.println(1);
else
System.out.println(0);
}
}
// This code is contributed by
// Surendra_Gangwar
Python3
# Python3 program to compare two integers
# without any comparison operator.
# Function return true if
# A ^ B > 0 else false
def EqualNumber(A, B):
return ( A ^ B )
# Driver Code
A = 5; B = 6
print(int(not(EqualNumber(A, B))))
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# program to compare two integers
// without any comparison operator.
using System;
class GFG
{
// function return true if
// A ^ B > 0 else false
static bool EqualNumber(int A, int B)
{
if(( A ^ B ) > 0)
return true;
else
return false;
}
// Driver Code
public static void Main()
{
int A = 5 , B = 6;
if(!EqualNumber(A, B) == false)
Console.WriteLine("0");
else
Console.WriteLine("1");
}
}
// This code is contributed
// by Akanksha Rai
PHP
0 else false
function EqualNumber($A, $B)
{
return ( $A ^ $B ) ;
}
// Driver Code
$A = 5 ;
$B = 6;
echo ((int)!(EqualNumber($A, $B))) . "\n";
// This code is contributed
// by ChitraNayal
?>
Javascript
输出:
0