📜  计算从1到N的所有数字中的总设置位|套装3

📅  最后修改于: 2021-04-23 17:38:46             🧑  作者: Mango

给定正整数N ,任务是对从1N的所有数字的二进制表示形式的置位总数进行计数。

例子:

方法:此问题的解决方案已在本文的Set 1和Set 2中发布。在这里,讨论了一种基于动态编程的方法。

  • 基本情况: 0和1中的置位位数分别为0和1。
  • 现在,对于范围[2,N]中的每个元素i ,如果i均是偶数,则它将具有与i / 2相同的置位位数,因为要获取数字i,我们只需将数字i / 2移一即可。移位时,置位位数不变。
  • 同样,如果i是奇数则它会具有1个另外的组位在0i位置– 1这是偶数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// set bits in all the integers
// from the range [1, n]
int countSetBits(int n)
{
 
    // To store the required count
    // of the set bits
    int cnt = 0;
 
    // To store the count of set
    // bits in every integer
    vector setBits(n + 1);
 
    // 0 has no set bit
    setBits[0] = 0;
 
    // 1 has a single set bit
    setBits[1] = 1;
 
    // For the rest of the elements
    for (int i = 2; i <= n; i++) {
 
        // If current element i is even then
        // it has set bits equal to the count
        // of the set bits in i / 2
        if (i % 2 == 0) {
            setBits[i] = setBits[i / 2];
        }
 
        // Else it has set bits equal to one
        // more than the previous element
        else {
            setBits[i] = setBits[i - 1] + 1;
        }
    }
 
    // Sum all the set bits
    for (int i = 0; i <= n; i++) {
        cnt = cnt + setBits[i];
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    int n = 6;
 
    cout << countSetBits(n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the count of
// set bits in all the integers
// from the range [1, n]
static int countSetBits(int n)
{
 
    // To store the required count
    // of the set bits
    int cnt = 0;
 
    // To store the count of set
    // bits in every integer
    int []setBits = new int[n + 1];
 
    // 0 has no set bit
    setBits[0] = 0;
 
    // 1 has a single set bit
    setBits[1] = 1;
 
    // For the rest of the elements
    for (int i = 2; i <= n; i++)
    {
 
        // If current element i is even then
        // it has set bits equal to the count
        // of the set bits in i / 2
        if (i % 2 == 0)
        {
            setBits[i] = setBits[i / 2];
        }
 
        // Else it has set bits equal to one
        // more than the previous element
        else
        {
            setBits[i] = setBits[i - 1] + 1;
        }
    }
 
    // Sum all the set bits
    for (int i = 0; i <= n; i++)
    {
        cnt = cnt + setBits[i];
    }
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 6;
 
    System.out.println(countSetBits(n));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
 
# Function to return the count of
# set bits in all the integers
# from the range [1, n]
def countSetBits(n):
 
    # To store the required count
    # of the set bits
    cnt = 0
 
    # To store the count of set
    # bits in every integer
    setBits = [0 for x in range(n + 1)]
 
    # 0 has no set bit
    setBits[0] = 0
 
    # 1 has a single set bit
    setBits[1] = 1
 
    # For the rest of the elements
    for i in range(2, n + 1):
 
        # If current element i is even then
        # it has set bits equal to the count
        # of the set bits in i / 2
        if (i % 2 == 0):
            setBits[i] = setBits[i // 2]
             
        # Else it has set bits equal to one
        # more than the previous element
        else:
            setBits[i] = setBits[i - 1] + 1
 
    # Sum all the set bits
    for i in range(0, n + 1):
        cnt = cnt + setBits[i]
     
    return cnt
 
# Driver code
n = 6
print(countSetBits(n))
 
# This code is contributed by Sanjit Prasad


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the count of
    // set bits in all the integers
    // from the range [1, n]
    static int countSetBits(int n)
    {
     
        // To store the required count
        // of the set bits
        int cnt = 0;
     
        // To store the count of set
        // bits in every integer
        int []setBits = new int[n + 1];
     
        // 0 has no set bit
        setBits[0] = 0;
     
        // 1 has a single set bit
        setBits[1] = 1;
     
        // For the rest of the elements
        for (int i = 2; i <= n; i++)
        {
     
            // If current element i is even then
            // it has set bits equal to the count
            // of the set bits in i / 2
            if (i % 2 == 0)
            {
                setBits[i] = setBits[i / 2];
            }
     
            // Else it has set bits equal to one
            // more than the previous element
            else
            {
                setBits[i] = setBits[i - 1] + 1;
            }
        }
     
        // Sum all the set bits
        for (int i = 0; i <= n; i++)
        {
            cnt = cnt + setBits[i];
        }
        return cnt;
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 6;
     
        Console.WriteLine(countSetBits(n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
9