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

📅  最后修改于: 2021-10-25 09:17:50             🧑  作者: 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程