原文:

给定一个由 n 个整数组成的数组 arr[] ,任务是在每个数组元素的二进制表示中找到设置位计数的乘积。

示例:

输入: arr[] = {3,2,4,1,5} 输出: 4 解释: 数组元素的二进制表示分别为{3,2,4,1,5}分别为{“11”,“10”,“100”,“1”,“101”}。 因此,集合比特数的乘积= (2 * 1 * 1 * 1 * 2) = 4。

输入: arr[] = {10,11,12 } t3】输出: 12

方法:给定的问题可以通过计算每个数组元素的二进制表示中的总位数来解决。按照以下步骤解决问题:

  • 初始化一个变量,比如产品,来存储结果产品。
  • 遍历给定数组 arr[] ,并执行以下步骤:
  • 完成上述步骤后,打印产品的值作为结果。

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to count the
// set bits in an integer
int countbits(int n)
{
    // stores the count of set bits
    int count = 0;
    // iterate while n is not equal to 0
    while (n != 0) {
        // increment count by 1
        if (n & 1)
            count  ;
        // divide n by 2
        n = n / 2;
    }
    // return the total count obtained
    return count;
}
// function to find the product
// of count of set bits present
// in each element of an array
int bitproduct(int arr[], int n)
{
    // stores the resultant product
    int product = 1;
    // traverse the array arr[]
    for (int i = 0; i < n; i  ) {
        // stores the count
        // of set bits of arr[i]
        int bits = countbits(arr[i]);
        // update the product
        product *= bits;
    }
    // return the resultant product
    return product;
}
// driver code
int main()
{
    int arr[] = { 3, 2, 4, 1, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << bitproduct(arr, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class gfg {
    // function to count the
    // set bits in an integer
    static int countbits(int n)
    {
        // stores the count of set bits
        int count = 0;
        // iterate while n is not equal to 0
        while (n != 0) {
            // increment count by 1
            if ((n & 1) != 0)
                count  ;
            // divide n by 2
            n = n / 2;
        }
        // return the total count obtained
        return count;
    }
    // function to find the product
    // of count of set bits present
    // in each element of an array
    static int bitproduct(int arr[], int n)
    {
        // stores the resultant product
        int product = 1;
        // traverse the array arr[]
        for (int i = 0; i < n; i  ) {
            // stores the count
            // of set bits of arr[i]
            int bits = countbits(arr[i]);
            // update the product
            product *= bits;
        }
        // return the resultant product
        return product;
    }
    // driver code
    public static void main(string[] args)
    {
        int arr[] = { 3, 2, 4, 1, 5 };
        int n = arr.length;
        system.out.print(bitproduct(arr, n));
    }
}
// this code is contributed by kingash.

python 3

# python3 program for the above approach
# function to count the
# set bits in an integer
def countbits(n):
    # stores the count of set bits
    count = 0
    # iterate while n is not equal to 0
    while (n != 0):
        # increment count by 1
        if (n & 1):
            count  = 1
        # divide n by 2
        n = n // 2
    # return the total count obtained
    return count
# function to find the product
# of count of set bits present
# in each element of an array
def bitproduct(arr, n):
    # stores the resultant product
    product = 1
    # traverse the array arr[]
    for i in range(n):
        # stores the count
        # of set bits of arr[i]
        bits = countbits(arr[i])
        # update the product
        product *= bits
    # return the resultant product
    return product
# driver code
if __name__ == '__main__':
    arr = [3, 2, 4, 1, 5]
    n = len(arr)
    print(bitproduct(arr, n))
    # this code is contributed by mohit kumar 29.

c

// c# program for the above approach
using system;
public class gfg {
    // function to count the
    // set bits in an integer
    static int countbits(int n)
    {
        // stores the count of set bits
        int count = 0;
        // iterate while n is not equal to 0
        while (n != 0) {
            // increment count by 1
            if ((n & 1) != 0)
                count  ;
            // divide n by 2
            n = n / 2;
        }
        // return the total count obtained
        return count;
    }
    // function to find the product
    // of count of set bits present
    // in each element of an array
    static int bitproduct(int[] arr, int n)
    {
        // stores the resultant product
        int product = 1;
        // traverse the array arr[]
        for (int i = 0; i < n; i  ) {
            // stores the count
            // of set bits of arr[i]
            int bits = countbits(arr[i]);
            // update the product
            product *= bits;
        }
        // return the resultant product
        return product;
    }
    // driver code
    public static void main(string[] args)
    {
        int[] arr = { 3, 2, 4, 1, 5 };
        int n = arr.length;
        console.write(bitproduct(arr, n));
    }
}
// this code is contributed by ukasp.

java 描述语言


output: 

4

时间复杂度: o(n * log m),m 是 阵的最大元素 辅助空间: o(1)