📜  检查两个数是相等的,而无需使用运算符

📅  最后修改于: 2021-05-25 10:33:59             🧑  作者: Mango

以下内容禁止使用
1)比较运算符
2)字符串函数
例子:

Input : num1 = 1233, num2 - 1233
Output : Same

Input : num1 = 223, num2 = 233
Output : Not Same

方法1:想法是使用XOR运算符。如果数字相同,则两个数字的XOR为0,否则为非零。

C++
#include 
using namespace std;
  
// Finds if a and b are same.
void areSame(int a, int b)
{
   if (a^b)
       cout << "Not Same";
   else
       cout << "Same";
}
  
int main()
{ 
   areSame(10, 20);
}


Java
class GFG
{
    // Finds if a and b are same
    static void areSame(int a,int b)
    {
        if( (a ^ b) != 0 )
            System.out.println("Not Same");
        else
            System.out.println("Same");
    }
 
    public static void main(String args[])
    {
        areSame(10,20);
    }
     
}
// This code is contributed by Sumit Ghosh


Python
# Finds if a and b are same.
def areSame(a, b):
   if (a ^ b):
       print "Not Same"
   else:
       print "Same"
   
# Driver code
areSame(10, 20)
 
# This code is submitted by Sachin Bisht


C#
// C# program to check if 2
// numbers are same
using System;
 
class GFG
{
    // Finds if a and b are same
    static void areSame(int a, int b)
    {
        if( (a ^ b) != 0 )
          Console.Write("Not Same");
        else
          Console.Write("Same");
    }
    
    // Driver code
    public static void Main()
    {
                
        // Calling Function
        areSame(10, 20);
    }
     
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


C++
// CPP code to check if 2 numbers are same
#include 
using namespace std;
 
// Finds if a and b are same
void areSame(int a, int b)
{
    if (!(a - b))
        cout << "Same";
    else
        cout << "Not Same";
}
 
// Driver code
int main()
{   
     areSame(10, 20);   
    return 0;
}


Java
// Java code to check if 2 numbers are same
class GFG{
 
    // Finds if a and b are same
    static void areSame(int a, int b)
    {
        if ((a - b) == 0)
            System.out.println("Same");
        else
            System.out.println("Not Same");
    }
  
    // Driver code
    public static void main(String args[])
    {   
        areSame(10, 20);   
     
    }
}
//This code is contributed by Sumit Ghosh


Python
# Python code to check if 2 numbers are same
 
# Finds if a and b are same
def areSame(a, b):
    if (not(a - b)):
        print "Same"
    else:
        print "Not Same"
# Driver code
areSame(10, 20)
 
# This code is submitted by Sachin Bisht


C#
// C# code to check if 2
// numbers are same
using System;
 
class GFG
{
 
    // Finds if a and b are same
    static void areSame(int a, int b)
    {
        if ((a - b) == 0)
            Console.Write("Same");
        else
            Console.Write("Not Same");
    }
 
    // Driver code
    public static void Main()
    {
         
        // Calling Function
        areSame(10, 20);
    }
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


输出:

Not Same

方法2:我们可以减去数字。相同的数字产生0。如果答案不为0,则数字不相同。

C++

// CPP code to check if 2 numbers are same
#include 
using namespace std;
 
// Finds if a and b are same
void areSame(int a, int b)
{
    if (!(a - b))
        cout << "Same";
    else
        cout << "Not Same";
}
 
// Driver code
int main()
{   
     areSame(10, 20);   
    return 0;
}

Java

// Java code to check if 2 numbers are same
class GFG{
 
    // Finds if a and b are same
    static void areSame(int a, int b)
    {
        if ((a - b) == 0)
            System.out.println("Same");
        else
            System.out.println("Not Same");
    }
  
    // Driver code
    public static void main(String args[])
    {   
        areSame(10, 20);   
     
    }
}
//This code is contributed by Sumit Ghosh

Python

# Python code to check if 2 numbers are same
 
# Finds if a and b are same
def areSame(a, b):
    if (not(a - b)):
        print "Same"
    else:
        print "Not Same"
# Driver code
areSame(10, 20)
 
# This code is submitted by Sachin Bisht

C#

// C# code to check if 2
// numbers are same
using System;
 
class GFG
{
 
    // Finds if a and b are same
    static void areSame(int a, int b)
    {
        if ((a - b) == 0)
            Console.Write("Same");
        else
            Console.Write("Not Same");
    }
 
    // Driver code
    public static void Main()
    {
         
        // Calling Function
        areSame(10, 20);
    }
}
 
// This code is contributed by Nitin Mittal.

的PHP


Java脚本


输出:

Not Same