以下是不允许使用的 1) 算术和比较运算符 2) 字符串函数
null
方法1: 这个想法是使用XOR运算符。如果两个数相同,则两个数的XOR为0,否则为非零。
C++
// C++ program to check if two numbers // are equal without using arithmetic // and comparison operators #include <iostream> using namespace std; // Function to check if two // numbers are equal using // XOR operator void areSame( int a, int b) { if (a ^ b) cout << "Not Same" ; else cout << "Same" ; } // Driver Code int main() { // Calling function areSame(10, 20); } |
JAVA
// Java program to check if two numbers // are equal without using arithmetic // and comparison operators class GFG { // Function to check if two // numbers are equal using // XOR operator static void areSame( int a, int b) { if ((a ^ b) != 0 ) System.out.print( "Not Same" ); else System.out.print( "Same" ); } // Driver Code public static void main(String[] args) { // Calling function areSame( 10 , 20 ); } } // This code is contributed by Smitha |
Python3
# Python3 program to check if two numbers # are equal without using arithmetic # and comparison operators def areSame(a, b): # Function to check if two # numbers are equal using # XOR operator if ((a ^ b) ! = 0 ): print ( "Not Same" ) else : print ( "Same" ) # Driver Code areSame( 10 , 20 ) # This code is contributed by Smitha |
C#
// C# program to check if two numbers // are equal without using arithmetic // and comparison operators using System; class GFG { // Function to check if two // numbers are equal using // XOR operator 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(String[] args) { // Calling function areSame(10, 20); } } // This code is contributed by Smitha |
PHP
<?php // PHP program to check if // two numbers are equal // without using arithmetic // and comparison operators // Function to check if two // numbers are equal using // XOR operator function areSame( $a , $b ) { if ( $a ^ $b ) echo "Not Same" ; else echo "Same" ; } // Driver Code // Calling function areSame(10, 20); // This code is contributed // by nitin mittal. ?> |
Javascript
<script> // Javascript program to check if two numbers // are equal without using arithmetic and // comparison operators // Function to check if two // numbers are equal using // XOR operator function areSame(a, b) { if ((a ^ b) != 0) document.write( "Not Same" ); else document.write( "Same" ); } // Driver Code areSame(10, 20); // This code is contributed by shikhasingrajput </script> |
输出:
Not Same
时间复杂性: O(1)
辅助空间: O(1)
方法2: 这里的想法是使用补码(~)和位“&”运算符。
C++
// C++ program to check if two numbers // are equal without using arithmetic // and comparison operators #include <iostream> using namespace std; // Function to check if two // numbers are equal using // using ~ complement and & operator. void areSame( int a, int b) { if ((a & ~b) == 0) cout << "Same" ; else cout << "Not Same" ; } // Driver Code int main() { // Calling function areSame(10, 10); // This Code is improved by Sonu Kumar Pandit } |
JAVA
// Java program to check if two numbers // are equal without using arithmetic // and comparison operators class GFG { // Function to check if two // numbers are equal using // using ~ complement and & operator. static void areSame( int a, int b) { if ((a & ~b) == 0 && (~a & b) == 0 ) System.out.print( "Same" ); else System.out.print( "Not Same" ); } // Driver Code public static void main(String args[]) { // Calling function areSame( 10 , 20 ); } } // This code is contributed // by Akanksha Rai |
Python3
# Python3 program to check if two numbers # are equal without using arithmetic # and comparison operators # Function to check if two # numbers are equal using # using ~ complement and & operator. def areSame(a, b): if ((a & ~b) = = 0 and (~a & b) = = 0 ): print ( "Same" ) else : print ( "Not Same" ) # Calling function areSame( 10 , 20 ) # This code is contributed by Rajput-Ji |
C#
// C# program to check if two numbers // are equal without using arithmetic // and comparison operators using System; class GFG { // Function to check if two // numbers are equal using // using ~ complement and & operator. static void areSame( int a, int b) { if ((a & ~b) == 0 && (~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 Akanksha Rai |
PHP
<?php // PHP program to check if two numbers // are equal without using arithmetic // and comparison operators // Function to check if two // numbers are equal using // using ~ complement and & operator. function areSame( $a , $b ) { if (( $a & ~ $b )==0 && (~ $a & $b )==0) echo "Same" ; else echo "Not Same" ; } // Driver Code // Calling function areSame(1, 1); // This code is contributed by ita_c ?> |
Javascript
<script> // Javascript program to check if two numbers // are equal without using arithmetic // and comparison operators // Function to check if two // Numbers are equal using // using ~ complement and & operator. function areSame(a, b) { if ((a & ~b) == 0 && (~a & b) == 0) document.write( "Same" ); else document.write( "Not Same" ); } // Driver Code // Calling function areSame(10, 20); // This code is contributed by gauravrajput1 </script> |
输出:
Not Same
时间复杂性: O(1)
辅助空间: O(1)
资料来源: https://www.geeksforgeeks.org/count-of-n-digit-numbers-whose-sum-of-digits-equals-to-given-sum/ 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END