📌  相关文章
📜  C程序以数字的二进制表示形式对零和一进行计数

📅  最后修改于: 2021-05-25 01:54:41             🧑  作者: Mango

给定数字N ,任务是编写C程序以N的二进制表示形式对01的数目进行计数。

例子:

方法1-天真的方法:想法是遍历N的二进制表示形式中的所有位,如果当前位为‘0’ ,则递增0s的计数,否则递增1s的计数。

下面是上述方法的实现:

C
// C program for the above approach
#include 
 
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
    // Initialise count variables
    int count0 = 0, count1 = 0;
 
    // Iterate through all the bits
    while (N > 0) {
 
        // If current bit is 1
        if (N & 1) {
            count1++;
        }
 
        // If current bit is 0
        else {
            count0++;
        }
 
        N = N >> 1;
    }
 
    // Print the count
    printf("Count of 0s in N is %d\n", count0);
    printf("Count of 1s in N is %d\n", count1);
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 9;
 
    // Function Call
    count1s0s(N);
    return 0;
}


C
// C program for the above approach
#include 
#include 
 
// Recursive approach to find the
// number of set bit in 1
int recursiveCount(int N)
{
 
    // Base Case
    if (N == 0) {
        return 0;
    }
 
    // Return recursively
    return (N & 1) + recursiveCount(N >> 1);
}
 
// Function to find 1s complement
int onesComplement(int n)
{
    // Find number of bits in the
    // given integer
    int N = floor(log2(n)) + 1;
 
    // XOR the given integer with
    // pow(2, N) - 1
    return ((1 << N) - 1) ^ n;
}
 
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
    // Initialise the count variables
    int count0, count1;
 
    // Function call to find the number
    // of set bits in N
    count1 = recursiveCount(N);
 
    // Function call to find 1s complement
    N = onesComplement(N);
 
    // Function call to find the number
    // of set bits in 1s complement of N
    count0 = recursiveCount(N);
 
    // Print the count
    printf("Count of 0s in N is %d\n", count0);
    printf("Count of 1s in N is %d\n", count1);
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 5;
 
    // Function Call
    count1s0s(N);
    return 0;
}


C
// C program for the above approach
#include 
#include 
 
// Function to find 1s complement
int onesComplement(int n)
{
    // Find number of bits in the
    // given integer
    int N = floor(log2(n)) + 1;
 
    // XOR the given integer with
    // pow(2, N) - 1
    return ((1 << N) - 1) ^ n;
}
 
// Function to implement count of
// set bits using Brian Kernighan’s
// Algorithm
int countSetBits(int n)
{
    // Initialise count
    int count = 0;
 
    // Iterate untill n is 0
    while (n) {
        n &= (n - 1);
        count++;
    }
 
    // Return the final count
    return count;
}
 
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
    // Initialise the count variables
    int count0, count1;
 
    // Function call to find the number
    // of set bits in N
    count1 = countSetBits(N);
 
    // Function call to find 1s complement
    N = onesComplement(N);
 
    // Function call to find the number
    // of set bits in 1s complement of N
    count0 = countSetBits(N);
 
    // Print the count
    printf("Count of 0s in N is %d\n", count0);
    printf("Count of 1s in N is %d\n", count1);
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 5;
 
    // Function Call
    count1s0s(N);
    return 0;
}


输出
Count of 0s in N is 2
Count of 1s in N is 2

时间复杂度: O(log N)

方法2 –递归方法:上述方法也可以使用递归来实现。

下面是上述方法的实现:

C

// C program for the above approach
#include 
#include 
 
// Recursive approach to find the
// number of set bit in 1
int recursiveCount(int N)
{
 
    // Base Case
    if (N == 0) {
        return 0;
    }
 
    // Return recursively
    return (N & 1) + recursiveCount(N >> 1);
}
 
// Function to find 1s complement
int onesComplement(int n)
{
    // Find number of bits in the
    // given integer
    int N = floor(log2(n)) + 1;
 
    // XOR the given integer with
    // pow(2, N) - 1
    return ((1 << N) - 1) ^ n;
}
 
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
    // Initialise the count variables
    int count0, count1;
 
    // Function call to find the number
    // of set bits in N
    count1 = recursiveCount(N);
 
    // Function call to find 1s complement
    N = onesComplement(N);
 
    // Function call to find the number
    // of set bits in 1s complement of N
    count0 = recursiveCount(N);
 
    // Print the count
    printf("Count of 0s in N is %d\n", count0);
    printf("Count of 1s in N is %d\n", count1);
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 5;
 
    // Function Call
    count1s0s(N);
    return 0;
}
输出
Count of 0s in N is 1
Count of 1s in N is 2

时间复杂度: O(log N)

方法3 –使用Brian Kernighan的算法
我们可以使用以下步骤找到设置位的数量:

  • 初始化计数为0
  • 如果N> 0 ,则将N更新为N&(N – 1),因为这将从右侧取消设置最大的位,如下所示:
if N = 10;
Binary representation of N     = 1010
Binary representation of N - 1 = 1001
-------------------------------------
Logical AND of N and N - 1     = 1000

  • 增加以上步骤的计数,并重复以上步骤,直到l N变为0。

要在N的二进制表示形式中找到0的计数,请使用上述方法找到N的一个补数并找到置位的计数。

下面是上述方法的实现:

C

// C program for the above approach
#include 
#include 
 
// Function to find 1s complement
int onesComplement(int n)
{
    // Find number of bits in the
    // given integer
    int N = floor(log2(n)) + 1;
 
    // XOR the given integer with
    // pow(2, N) - 1
    return ((1 << N) - 1) ^ n;
}
 
// Function to implement count of
// set bits using Brian Kernighan’s
// Algorithm
int countSetBits(int n)
{
    // Initialise count
    int count = 0;
 
    // Iterate untill n is 0
    while (n) {
        n &= (n - 1);
        count++;
    }
 
    // Return the final count
    return count;
}
 
// Function to count the number of 0s
// and 1s in binary representation of N
void count1s0s(int N)
{
    // Initialise the count variables
    int count0, count1;
 
    // Function call to find the number
    // of set bits in N
    count1 = countSetBits(N);
 
    // Function call to find 1s complement
    N = onesComplement(N);
 
    // Function call to find the number
    // of set bits in 1s complement of N
    count0 = countSetBits(N);
 
    // Print the count
    printf("Count of 0s in N is %d\n", count0);
    printf("Count of 1s in N is %d\n", count1);
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 5;
 
    // Function Call
    count1s0s(N);
    return 0;
}
输出
Count of 0s in N is 1
Count of 1s in N is 2

时间复杂度: O(log N)

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。