📌  相关文章
📜  检查给定数字的二进制表示形式及其补码是否为字谜

📅  最后修改于: 2021-04-23 05:29:08             🧑  作者: Mango

给定一个正数,您需要检查它的补码和数字是否是字谜。
例子:

Input : a = 4294967295
Output : Yes
Binary representation of 'a' and it's
complement are anagrams of each other

Input : a = 4
Output : No

简单方法:在这种方法中,可以计算数量的补数。
1.使用简单的十进制到二进制表示技术查找数字的二进制表示形式及其补码。
2.对两个二进制表示形式进行排序并进行比较,以检查它们是否为字谜。

CPP
// A simple C++ program to check if binary
// representations of a number and it's
// complement are anagram.
#include 
#define ull unsigned long long int
using namespace std;
 
const int ULL_SIZE = 8*sizeof(ull);
 
bool isComplementAnagram(ull a)
{
    ull b = ~a; // Finding complement of a;
 
    // Find reverse binary representation of a.
    bool binary_a[ULL_SIZE] = { 0 };
    for (int i=0; a > 0; i++)
    {
        binary_a[i] = a % 2;
        a /= 2;
    }
 
    // Find reverse binary representation
    // of complement.
    bool binary_b[ULL_SIZE] = { 0 };
    for (int i=0; b > 0; i++)
    {
        binary_b[i] = b % 2;
        b /= 2;
    }
 
    // Sort binary representations and compare
    // after sorting.
    sort(binary_a, binary_a + ULL_SIZE);
    sort(binary_b, binary_b + ULL_SIZE);
    for (int i = 0; i < ULL_SIZE; i++)
        if (binary_a[i] != binary_b[i])
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    ull a = 4294967295;
    cout << isComplementAnagram(a) << endl;
    return 0;
}


C++
// An efficient C++ program to check if binary
// representations of a number and it's complement are anagram.
#include 
#define ull unsigned long long int
using namespace std;
 
const int ULL_SIZE = 8*sizeof(ull);
 
// Returns true if binary representations of
// a and b are anagram.
bool bit_anagram_check(ull a)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a. If number
    // of 1s is half of total bits, return true.
    return (_popcnt64(a) == (ULL_SIZE >> 1));
}
 
int main()
{
    ull a = 4294967295;
    cout << bit_anagram_check(a) << endl;
    return 0;
}


Java
// An efficient Java program to check if binary
// representations of a number and it's complement are anagram.
class GFG
{
 
static byte longSize = 8;
static int ULL_SIZE = 8*longSize;
 
// Returns true if binary representations of
// a and b are anagram.
static boolean bit_anagram_check(long a)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a. If number
    // of 1s is half of total bits, return true.
    return (Integer.bitCount((int)a) == (ULL_SIZE >> 1));
}
 
// Driver code
public static void main(String[] args)
{
long a = 4294967295L;
    System.out.println(bit_anagram_check(a));
}
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# An efficient Python3 program to check
# if binary representations of a number
# and it's complement are anagram.
ULL_SIZE = 64
 
# Returns true if binary representations of
# a and b are anagram.
def bit_anagram_check(a):
 
    #_popcnt64(a) gives number of 1's present
    # in binary representation of a. If number
    # of 1s is half of total bits, return true.
    return (bin(a).count("1") == (ULL_SIZE >> 1))
 
# Driver Code
a = 4294967295
print(int(bit_anagram_check(a)))
 
# This code is contributed by Mohit Kumar


C#
// An efficient C# program to check
// if binary representations of
// a number and it's complement
// are anagram.
using System;
 
class GFG
{
 
static byte longSize = 8;
static int ULL_SIZE = 8*longSize;
 
// Returns true if binary representations of
// a and b are anagram.
static bool bit_anagram_check(long a)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a. If number
    // of 1s is half of total bits, return true.
    return (BitCount((int)a) == (ULL_SIZE >> 1));
}
 
static int BitCount(int n)
{
    int count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
 
// Driver code
public static void Main(String[] args)
{
    long a = 4294967295L;
    Console.WriteLine(bit_anagram_check(a));
}
}
 
// This code has been contributed by 29AjayKumar


PHP
> 1));
}
 
function BitCount($n)
{
    $count = 0;
    while ($n != 0)
    {
        $count++;
        $n &= ($n - 1);
    }
    return $count;
}
 
// Driver code
$a = 4294967295;
echo(bit_anagram_check($a));
 
// This code contributed by Rajput-Ji
?>


Javascript


输出:

1

高效方法:只需计算给定数字的位表示中存在的1的数目即可。如果存在的1的数目为32,则它的补码也将以位表示形式包含32 1,并且它们将是彼此的字谜。

C++

// An efficient C++ program to check if binary
// representations of a number and it's complement are anagram.
#include 
#define ull unsigned long long int
using namespace std;
 
const int ULL_SIZE = 8*sizeof(ull);
 
// Returns true if binary representations of
// a and b are anagram.
bool bit_anagram_check(ull a)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a. If number
    // of 1s is half of total bits, return true.
    return (_popcnt64(a) == (ULL_SIZE >> 1));
}
 
int main()
{
    ull a = 4294967295;
    cout << bit_anagram_check(a) << endl;
    return 0;
}

Java

// An efficient Java program to check if binary
// representations of a number and it's complement are anagram.
class GFG
{
 
static byte longSize = 8;
static int ULL_SIZE = 8*longSize;
 
// Returns true if binary representations of
// a and b are anagram.
static boolean bit_anagram_check(long a)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a. If number
    // of 1s is half of total bits, return true.
    return (Integer.bitCount((int)a) == (ULL_SIZE >> 1));
}
 
// Driver code
public static void main(String[] args)
{
long a = 4294967295L;
    System.out.println(bit_anagram_check(a));
}
}
 
/* This code contributed by PrinciRaj1992 */

Python3

# An efficient Python3 program to check
# if binary representations of a number
# and it's complement are anagram.
ULL_SIZE = 64
 
# Returns true if binary representations of
# a and b are anagram.
def bit_anagram_check(a):
 
    #_popcnt64(a) gives number of 1's present
    # in binary representation of a. If number
    # of 1s is half of total bits, return true.
    return (bin(a).count("1") == (ULL_SIZE >> 1))
 
# Driver Code
a = 4294967295
print(int(bit_anagram_check(a)))
 
# This code is contributed by Mohit Kumar

C#

// An efficient C# program to check
// if binary representations of
// a number and it's complement
// are anagram.
using System;
 
class GFG
{
 
static byte longSize = 8;
static int ULL_SIZE = 8*longSize;
 
// Returns true if binary representations of
// a and b are anagram.
static bool bit_anagram_check(long a)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a. If number
    // of 1s is half of total bits, return true.
    return (BitCount((int)a) == (ULL_SIZE >> 1));
}
 
static int BitCount(int n)
{
    int count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
 
// Driver code
public static void Main(String[] args)
{
    long a = 4294967295L;
    Console.WriteLine(bit_anagram_check(a));
}
}
 
// This code has been contributed by 29AjayKumar

的PHP

> 1));
}
 
function BitCount($n)
{
    $count = 0;
    while ($n != 0)
    {
        $count++;
        $n &= ($n - 1);
    }
    return $count;
}
 
// Driver code
$a = 4294967295;
echo(bit_anagram_check($a));
 
// This code contributed by Rajput-Ji
?>

Java脚本


输出:

1