📜  查找N的位的可能排列

📅  最后修改于: 2021-04-23 22:04:54             🧑  作者: Mango

给定整数N ,任务是查找N的位是否可以交替排列,即0101…10101… 。假设N表示为32位整数。

例子:

方法:由于给定整数必须用32位表示,并且1的数量必须等于其二进制表示形式中的0的数量,才能满足给定条件。因此,N中的置位位数必须为16,可以使用__builtin_popcount()轻松计算出

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int TOTAL_BITS = 32;
  
// Function that returns true if it is
// possible to arrange the bits of
// n in alternate fashion
bool isPossible(int n)
{
  
    // To store the count of 1s in the
    // binary representation of n
    int cnt = __builtin_popcount(n);
  
    // If the number set bits and the
    // number of unset bits is equal
    if (cnt == TOTAL_BITS / 2)
        return true;
    return false;
}
  
// Driver code
int main()
{
    int n = 524280;
  
    if (isPossible(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
      
class GFG
{
  
static int TOTAL_BITS = 32;
  
// Function that returns true if it is
// possible to arrange the bits of
// n in alternate fashion
static boolean isPossible(int n)
{
  
    // To store the count of 1s in the
    // binary representation of n
    int cnt = Integer.bitCount(n);
  
    // If the number set bits and the
    // number of unset bits is equal
    if (cnt == TOTAL_BITS / 2)
        return true;
    return false;
}
  
// Driver code
static public void main (String []arr)
{
    int n = 524280;
  
    if (isPossible(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
TOTAL_BITS = 32; 
  
# Function that returns true if it is 
# possible to arrange the bits of 
# n in alternate fashion 
def isPossible(n) :
  
    # To store the count of 1s in the 
    # binary representation of n 
    cnt = bin(n).count('1'); 
  
    # If the number set bits and the 
    # number of unset bits is equal 
    if (cnt == TOTAL_BITS // 2) :
        return True; 
          
    return False; 
  
# Driver code 
if __name__ == "__main__" : 
  
    n = 524280; 
  
    if (isPossible(n)) :
        print("Yes"); 
    else :
        print("No"); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach 
using System; 
      
class GFG
{
static int TOTAL_BITS = 32;
  
static int CountBits(int value)
{
    int count = 0;
    while (value != 0)
    {
        count++;
        value &= value - 1;
    }
    return count;
}
  
// Function that returns true if it is
// possible to arrange the bits of
// n in alternate fashion
static bool isPossible(int n)
{
  
    // To store the count of 1s in the
    // binary representation of n
    int cnt = CountBits(n);
  
    // If the number set bits and the
    // number of unset bits is equal
    if (cnt == TOTAL_BITS / 2)
        return true;
    return false;
}
  
// Driver code
public static void Main (String []arr)
{
    int n = 524280;
  
    if (isPossible(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by Mohit kumar


输出:
Yes