📌  相关文章
📜  检查是否可以通过置换A的二进制数字来形成B

📅  最后修改于: 2021-05-05 00:40:20             🧑  作者: Mango

给定两个整数AB ,任务是检查是否可以通过置换A的二进制数字来生成B的二进制表示形式。
例子:

方法:想法是对两个数字的二进制表示形式中的设置位数进行计数,现在如果它们相等,则答案为“是”,否则答案为“否”。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if the
// binary representation of b can be
// generated by permuting the
// binary digits of a
bool isPossible(int a, int b)
{
 
    // Find the count of set bits
    // in both the integers
    int cntA = __builtin_popcount(a);
    int cntB = __builtin_popcount(b);
 
    // If both the integers have
    // equal count of set bits
    if (cntA == cntB)
        return true;
    return false;
}
 
// Driver code
int main()
{
    int a = 3, b = 9;
 
    if (isPossible(a, b))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // recursive function to count set bits
    public static int countSetBits(int n)
    {
 
        // base case
        if (n == 0)
            return 0;
        else
 
            // if last bit set add 1 else add 0
            return (n & 1) + countSetBits(n >> 1);
    }
     
    // Function that returns true if the
    // binary representation of b can be
    // generated by permuting the
    // binary digits of a
    static boolean isPossible(int a, int b)
    {
     
        // Find the count of set bits
        // in both the integers
        int cntA = countSetBits(a);
        int cntB = countSetBits(b);
     
        // If both the integers have
        // equal count of set bits
        if (cntA == cntB)
            return true;
        return false;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a = 3, b = 9;
     
        if (isPossible(a, b))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# function to count set bits
def bitsoncount(x):
    return bin(x).count('1')
 
# Function that returns true if the
# binary representation of b can be
# generated by permuting the
# binary digits of a
def isPossible(a, b):
 
    # Find the count of set bits
    # in both the integers
    cntA = bitsoncount(a);
    cntB = bitsoncount(b);
 
    # If both the integers have
    # equal count of set bits
    if (cntA == cntB):
        return True
    return False
 
# Driver code
a = 3
b = 9
 
if (isPossible(a, b)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Sanjit Prasad


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
    // recursive function to count set bits
    public static int countSetBits(int n)
    {
 
        // base case
        if (n == 0)
            return 0;
        else
 
            // if last bit set.Add 1 else.Add 0
            return (n & 1) + countSetBits(n >> 1);
    }
     
    // Function that returns true if the
    // binary representation of b can be
    // generated by permuting the
    // binary digits of a
    static bool isPossible(int a, int b)
    {
     
        // Find the count of set bits
        // in both the integers
        int cntA = countSetBits(a);
        int cntB = countSetBits(b);
     
        // If both the integers have
        // equal count of set bits
        if (cntA == cntB)
            return true;
        return false;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int a = 3, b = 9;
     
        if (isPossible(a, b))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes