Dados dos enteros A y B. La tarea es verificar si A y B son iguales o no sin usar operadores de comparación.
Ejemplos:
Input : A = 5 , B = 6 Output : 0 Input : A = 5 , B = 5 Output : 1 Note : 1 = "YES" and 0 = "NO"
La idea es bastante simple, hacemos Xor de ambos elementos (A, B). si Xor es cero, entonces dos números son iguales, de lo contrario no.
A continuación se muestra la implementación de la idea anterior:
C++
// C++ program to compare two integers without // any comparison operator. #include<bits/stdc++.h> 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 without // 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
<?php // PHP program to compare two integers // without any comparison operator. // function return true if // A ^ B > 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
<script> // JavaScript program to compare two integers without // any comparison operator. // function return true if A ^ B > 0 else false function EqualNumber(A, B) { return ( A ^ B ) ; } // Driver program let A = 5 , B = 6; if(!EqualNumber(A, B) == false) document.write("0"); else document.write("1"); // This code is contributed by Surbhi Tyagi. </script>
0
Complejidad de tiempo: O(1)
Espacio auxiliar: O (1)
Referencia: http://stackoverflow.com/questions/476800/comparing-two-integers-without-any-comparison
2º Método :
Otro enfoque es usar «-» (operador menos/resta). Si el Resultado de la Resta entre los dos Números es “Cero”, entonces son iguales (devolvemos ‘1’), de lo contrario no son iguales (devolvemos ‘0’).
C++
// C++ program to compare two integers // without any comparison operator. #include <iostream> using namespace std; // Function return 0 if // A - B != 0 else return 1 int EqualNumber(int A, int B) { int diff = A - B; if(diff) return 0; else return 1; } // Driver Code int main() { int A = 5, B = 6; cout << EqualNumber(A, B); return 0; } // This code is contributed by kothavvsaakash
Python
# Python3 program to compare two integers # without any comparison operator. # Function return 0 if # A - B != 0 else return 1 def EqualNumber(A, B): diff = A - B if(diff): return 0 else: return 1 # Driver Code A = 5; B = 6 print((EqualNumber(A, B))) # This code is contributed by kothavvsaakash
0
Este artículo es una contribución de Nishant Singh . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA