📌  相关文章
📜  二进制表示形式中偶数为1的数字总和

📅  最后修改于: 2021-06-26 19:47:46             🧑  作者: Mango

给定大小为N的数组arr [] 。任务是找到所有数组元素的数字总和,这些数字的二进制表示形式中包含偶数1
例子:

方法 :
对每个数组元素的二进制表示形式中的1的数目进行计数,如果为偶数,则计算其数字的总和。
下面是上述方法的实现:

C++
// CPP program to find Sum of digits with even
// number of 1’s in their binary representation
#include 
using namespace std;
 
// Function to count and check the
// number of 1's is even or odd
int countOne(int n)
{
    int count = 0;
    while (n) {
        n = n & (n - 1);
        count++;
    }
 
    if (count % 2 == 0)
        return 1;
    else
        return 0;
}
 
// Function to calculate the sum
// of the digits of a number
int sumDigits(int n)
{
    int sum = 0;
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
 
    return sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 9, 15 };
     
    int n = sizeof(arr) / sizeof(arr[0]);
    int total_sum = 0;
 
    // Iterate through the array
    for (int i = 0; i < n; i++) {
        if (countOne(arr[i]))
            total_sum += sumDigits(arr[i]);
    }
 
    cout << total_sum << '\n';
     
    return 0;
}


Java
// Java program to find Sum of digits with even
// number of 1's in their binary representation
import java.util.*;
 
class GFG
{
 
// Function to count and check the
// number of 1's is even or odd
static int countOne(int n)
{
    int count = 0;
    while (n > 0)
    {
        n = n & (n - 1);
        count++;
    }
 
    if (count % 2 == 0)
        return 1;
    else
        return 0;
}
 
// Function to calculate the sum
// of the digits of a number
static int sumDigits(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum += n % 10;
        n /= 10;
    }
 
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 4, 9, 15 };
     
    int n = arr.length;
    int total_sum = 0;
 
    // Iterate through the array
    for (int i = 0; i < n; i++)
    {
        if (countOne(arr[i]) == 1)
            total_sum += sumDigits(arr[i]);
    }
    System.out.println(total_sum);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find Sum of digits with even
# number of 1’s in their binary representation
 
# Function to count and check the
# number of 1's is even or odd
def countOne(n):
    count = 0
    while (n):
        n = n & (n - 1)
        count += 1
 
    if (count % 2 == 0):
        return 1
    else:
        return 0
 
# Function to calculate the summ
# of the digits of a number
def summDigits(n):
    summ = 0
    while (n != 0):
        summ += n % 10
        n //= 10
 
    return summ
 
# Driver Code
arr = [4, 9, 15]
 
n = len(arr)
total_summ = 0
 
# Iterate through the array
for i in range(n):
    if (countOne(arr[i])):
        total_summ += summDigits(arr[i])
 
print(total_summ )
 
# This code is contributed by Mohit Kumar


C#
// C# program to find Sum of digits with even
// number of 1's in their binary representation
using System;
 
class GFG
{
 
// Function to count and check the
// number of 1's is even or odd
static int countOne(int n)
{
    int count = 0;
    while (n > 0)
    {
        n = n & (n - 1);
        count++;
    }
 
    if (count % 2 == 0)
        return 1;
    else
        return 0;
}
 
// Function to calculate the sum
// of the digits of a number
static int sumDigits(int n)
{
    int sum = 0;
    while (n != 0)
    {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 4, 9, 15 };
     
    int n = arr.Length;
    int total_sum = 0;
 
    // Iterate through the array
    for (int i = 0; i < n; i++)
    {
        if (countOne(arr[i]) == 1)
            total_sum += sumDigits(arr[i]);
    }
    Console.WriteLine(total_sum);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
15

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。