📜  设置位数与N相同

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

给定一个正整数N,找出有多少个严格小于N的正整数具有与N相同的置位位数。

例子 :

Input : 8
Output :3
Explanation: Binary representation of
8 : 1000, so number of set bits in 8 is 1.
So the integers less than 8 with same number 
of set bits are : 4, 2, 1

Input :1
Output :0

Input :4
Output :2

方法:

1. Using __builtin_popcount() inbuilt function, count set bits in N and store into a 
temp variable 
2. Iterate from n-1 to 1 and also count set bits in i using __builtin_popcount() 
function
3. Now, compare temp with __builtin_popcount(i)
4. If both are equal then increment counter variable
5. Return counter

下面是上述方法的实现。

C++
// CPP program to find numbers less than N
// that have same Number Of Set Bits As N
#include 
using namespace std;
  
int smallerNumsWithSameSetBits(int n)
{        
    // __builtin_popcount function that count
    // set bits in n
    int temp = __builtin_popcount(n);
      
    // Iterate from n-1 to 1
    int count = 0;
    for (int i = n - 1; i > 0; i--) {
          
        // check if the number of set bits 
        // equals to temp increment count        
        if (temp == __builtin_popcount(i))
            count++;
    }
    return count;
}
  
// Driver Code 
int main()
{
    int n = 4;
    cout << smallerNumsWithSameSetBits(n);
    return 0;
}


Java
// Java program to find numbers less than N
// that have same Number Of Set Bits As N
class GFG {
      
    // returns number of set bits in a number
    static int __builtin_popcount(int n)
    {
        int d, t = 0;
          
        while(n > 0)
        {
            d = n % 2;
            n = n / 2;
            if(d == 1)
                t++;
        }
        return t;
    }
      
    static int smallerNumsWithSameSetBits(int n)
    {     
        // __builtin_popcount function that count
        // set bits in n
        int temp = __builtin_popcount(n);
          
        // Iterate from n-1 to 1
        int count = 0;
        for (int i = n - 1; i > 0; i--) {
              
            // check if the number of set bits 
            // equals to temp increment count     
            if (temp == __builtin_popcount(i))
                count++;
        }
        return count;
    }
      
    // Driver Code 
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(
            smallerNumsWithSameSetBits(n));
    }
}
  
// This code is contributed by Arnab Kundu.


Python3
# Python3 program to find numbers 
# less than N that have same
# Number Of Set Bits As N
def __builtin_popcount(n) :
    t = 0    
    while(n > 0) :
        d = n % 2
        n = int(n / 2)
        if(d == 1) :
            t = t + 1
    return t
  
def smallerNumsWithSameSetBits(n) :    
      
    # __builtin_popcount function 
    # that count set bits in n
    temp = __builtin_popcount(n)
      
    # Iterate from n-1 to 1
    count = 0
    for i in range(n-1,0,-1) :     
          
        # check if the number of 
        # set bits equals to temp
        # increment count     
        if (temp == __builtin_popcount(i)) :
            count = count + 1
    return count
  
# Driver Code 
n = 4
print (smallerNumsWithSameSetBits(n))
  
# This code is contributed by 
# Manish Shaw(manishshaw1)


C#
// C# program to find numbers less than N
// that have same Number Of Set Bits As N
using System;
  
class GFG {
      
    // returns number of set bits in a number
    static int __builtin_popcount(int n)
    {
        int d, t = 0;
           
        while(n > 0)
        {
            d = n % 2;
            n = n / 2;
            if(d == 1)
                t++;
        }
        return t;
    }
      
    static int smallerNumsWithSameSetBits(int n)
    {     
        // __builtin_popcount function that count
        // set bits in n
        int temp = __builtin_popcount(n);
          
        // Iterate from n-1 to 1
        int count = 0;
        for (int i = n - 1; i > 0; i--) {
              
            // check if the number of set bits 
            // equals to temp increment count     
            if (temp == __builtin_popcount(i))
                count++;
        }
        return count;
    }
      
    // Driver Code 
    static public void Main(String []args)
    {
        int n = 4;
        Console.WriteLine(
            smallerNumsWithSameSetBits(n));
    }
}
  
// This code is contributed by Arnab Kundu.


PHP
 0)
    {
        $d = $n % 2;
        $n = intval($n / 2);
        if($d == 1)
            $t++;
    }
    return $t;
}
function smallerNumsWithSameSetBits($n)
{     
    // __builtin_popcount function 
    // that count set bits in n
    $temp = __builtin_popcount($n);
      
    // Iterate from n-1 to 1
    $count = 0;
    for ($i = $n - 1; $i > 0; $i--)
    {
          
        // check if the number of 
        // set bits equals to temp
        // increment count     
        if ($temp == __builtin_popcount($i))
            $count++;
    }
    return $count;
}
  
// Driver Code 
$n = 4;
echo (smallerNumsWithSameSetBits($n));
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>


输出:
2