📜  计算从0到N的每个数字的设置位数

📅  最后修改于: 2021-04-26 18:17:06             🧑  作者: Mango

给定一个非负整数N ,任务是查找从0N的每个数字的设置位数。

例子:

天真的方法:运行从0到N的循环,并使用内置的位计数函数__builtin_popcount(),找到所有必需整数中的设置位数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
void findSetBits(int n)
{
    for (int i = 0; i <= n; i++)
        cout << __builtin_popcount(i) << " ";
}
  
// Driver code
int main()
{
    int n = 5;
  
    findSetBits(n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
static void findSetBits(int n)
{
    for (int i = 0; i <= n; i++)
        System.out.print(Integer.bitCount(i) + " ");
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 5;
  
    findSetBits(n);
}
}
  
// This code is contributed by Rajput-Ji


Python 3
# Python 3 implementation of the approach
def count(n):
    count = 0
    while (n): 
        count += n & 1
        n >>= 1
    return count
  
# Function to find the count
# of set bits in all the
# integers from 0 to n
def findSetBits(n):
    for i in range(n + 1):
        print(count(i), end = " ")
      
# Driver code
if __name__ == '__main__':
    n = 5
  
    findSetBits(n)
  
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the approach 
using System; 
      
class GFG 
{ 
  
static int count(int n) 
    { 
        int count = 0; 
        while (n > 0)
        { 
            count += n & 1; 
            n >>= 1; 
        } 
        return count; 
    } 
      
// Function to find the count 
// of set bits in all the 
// integers from 0 to n 
static void findSetBits(int n) 
{ 
    for (int i = 0; i <= n; i++) 
        Console.Write(count(i)+" "); 
} 
  
// Driver code 
public static void Main(String []args)
{ 
    int n = 5; 
  
    findSetBits(n); 
} 
}
  
// This code is contributed by SHUBHAMSINGH10


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
void findSetBits(int n)
{
  
    // dp[i] will store the count
    // of set bits in i
    int dp[n + 1];
  
    // Initialise the dp array
    memset(dp, 0, sizeof(dp));
  
    // Count of set bits in 0 is 0
    cout << dp[0] << " ";
  
    // For every number starting from 1
    for (int i = 1; i <= n; i++) {
  
        // If current number is even
        if (i % 2 == 0) {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2)
            dp[i] = dp[i / 2];
        }
  
        // If current element is odd
        else {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2) + 1
            dp[i] = dp[i / 2] + 1;
        }
  
        // Print the count of set bits in i
        cout << dp[i] << " ";
    }
}
  
// Driver code
int main()
{
    int n = 5;
  
    findSetBits(n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
static void findSetBits(int n)
{
  
    // dp[i] will store the count
    // of set bits in i
    int []dp = new int[n + 1];
  
    // Count of set bits in 0 is 0
    System.out.print(dp[0] + " ");
  
    // For every number starting from 1
    for (int i = 1; i <= n; i++) 
    {
  
        // If current number is even
        if (i % 2 == 0) 
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2)
            dp[i] = dp[i / 2];
        }
  
        // If current element is odd
        else
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2) + 1
            dp[i] = dp[i / 2] + 1;
        }
  
        // Print the count of set bits in i
        System.out.print(dp[i] + " ");
    }
}
  
// Driver code
public static void main(String []args)
{
    int n = 5;
  
    findSetBits(n);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function to find the count of set bits 
# in all the integers from 0 to n 
def findSetBits(n) :
  
    # dp[i] will store the count 
    # of set bits in i 
    # Initialise the dp array
    dp = [0] * (n + 1); 
      
    # Count of set bits in 0 is 0 
    print(dp[0], end = " "); 
  
    # For every number starting from 1 
    for i in range(1, n + 1) :
  
        # If current number is even 
        if (i % 2 == 0) :
  
            # Count of set bits in i is equal to 
            # the count of set bits in (i / 2) 
            dp[i] = dp[i // 2]; 
  
        # If current element is odd 
        else :
  
            # Count of set bits in i is equal to 
            # the count of set bits in (i / 2) + 1 
            dp[i] = dp[i // 2] + 1; 
  
        # Print the count of set bits in i 
        print(dp[i], end = " "); 
  
# Driver code 
if __name__ == "__main__" : 
  
    n = 5; 
  
    findSetBits(n); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
static void findSetBits(int n)
{
  
    // dp[i] will store the count
    // of set bits in i
    int []dp = new int[n + 1];
  
    // Count of set bits in 0 is 0
    Console.Write(dp[0] + " ");
  
    // For every number starting from 1
    for (int i = 1; i <= n; i++) 
    {
  
        // If current number is even
        if (i % 2 == 0) 
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2)
            dp[i] = dp[i / 2];
        }
  
        // If current element is odd
        else
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2) + 1
            dp[i] = dp[i / 2] + 1;
        }
  
        // Print the count of set bits in i
        Console.Write(dp[i] + " ");
    }
}
  
// Driver code
public static void Main(String []args)
{
    int n = 5;
  
    findSetBits(n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
0 1 1 2 1 2

高效的方法:让我们写数字在(0,6)范围内的二进制表示形式。

因为,任何偶数都可以写为(2 * i) ,任何奇数都可以写为(2 * i + 1) ,其中i是自然数。
2、43、6在其二进制表示形式中具有相等的1,因为将任何数字乘以等于将其左移1(在此处阅读)。
同样,任何偶数2 * ii在其二进制表示形式中都将具有相等的1
1个在5(101)的数目为2的二进制表示+ 1等于1倍的数。因此,在任何奇数(2 * i + 1)的情况下,它将是( i的二进制表示形式中的1的个数)+ 1

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
void findSetBits(int n)
{
  
    // dp[i] will store the count
    // of set bits in i
    int dp[n + 1];
  
    // Initialise the dp array
    memset(dp, 0, sizeof(dp));
  
    // Count of set bits in 0 is 0
    cout << dp[0] << " ";
  
    // For every number starting from 1
    for (int i = 1; i <= n; i++) {
  
        // If current number is even
        if (i % 2 == 0) {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2)
            dp[i] = dp[i / 2];
        }
  
        // If current element is odd
        else {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2) + 1
            dp[i] = dp[i / 2] + 1;
        }
  
        // Print the count of set bits in i
        cout << dp[i] << " ";
    }
}
  
// Driver code
int main()
{
    int n = 5;
  
    findSetBits(n);
  
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
static void findSetBits(int n)
{
  
    // dp[i] will store the count
    // of set bits in i
    int []dp = new int[n + 1];
  
    // Count of set bits in 0 is 0
    System.out.print(dp[0] + " ");
  
    // For every number starting from 1
    for (int i = 1; i <= n; i++) 
    {
  
        // If current number is even
        if (i % 2 == 0) 
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2)
            dp[i] = dp[i / 2];
        }
  
        // If current element is odd
        else
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2) + 1
            dp[i] = dp[i / 2] + 1;
        }
  
        // Print the count of set bits in i
        System.out.print(dp[i] + " ");
    }
}
  
// Driver code
public static void main(String []args)
{
    int n = 5;
  
    findSetBits(n);
}
}
  
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the approach 
  
# Function to find the count of set bits 
# in all the integers from 0 to n 
def findSetBits(n) :
  
    # dp[i] will store the count 
    # of set bits in i 
    # Initialise the dp array
    dp = [0] * (n + 1); 
      
    # Count of set bits in 0 is 0 
    print(dp[0], end = " "); 
  
    # For every number starting from 1 
    for i in range(1, n + 1) :
  
        # If current number is even 
        if (i % 2 == 0) :
  
            # Count of set bits in i is equal to 
            # the count of set bits in (i / 2) 
            dp[i] = dp[i // 2]; 
  
        # If current element is odd 
        else :
  
            # Count of set bits in i is equal to 
            # the count of set bits in (i / 2) + 1 
            dp[i] = dp[i // 2] + 1; 
  
        # Print the count of set bits in i 
        print(dp[i], end = " "); 
  
# Driver code 
if __name__ == "__main__" : 
  
    n = 5; 
  
    findSetBits(n); 
  
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to find the count
// of set bits in all the
// integers from 0 to n
static void findSetBits(int n)
{
  
    // dp[i] will store the count
    // of set bits in i
    int []dp = new int[n + 1];
  
    // Count of set bits in 0 is 0
    Console.Write(dp[0] + " ");
  
    // For every number starting from 1
    for (int i = 1; i <= n; i++) 
    {
  
        // If current number is even
        if (i % 2 == 0) 
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2)
            dp[i] = dp[i / 2];
        }
  
        // If current element is odd
        else
        {
  
            // Count of set bits in i is equal to
            // the count of set bits in (i / 2) + 1
            dp[i] = dp[i / 2] + 1;
        }
  
        // Print the count of set bits in i
        Console.Write(dp[i] + " ");
    }
}
  
// Driver code
public static void Main(String []args)
{
    int n = 5;
  
    findSetBits(n);
}
}
  
// This code is contributed by 29AjayKumar
输出:
0 1 1 2 1 2