给定一个数组arr [] ,任务是从给定的具有偶数奇偶校验的数组中计算元素的总和,即使用按位运算运算符设置位的数量为偶数。
例子:
Input: arr[] = {2, 4, 3, 5, 9}
Output: 17
Only 3(0011), 5(0101) and 9(1001) have even parity
So 3 + 5 + 9 = 17
Input: arr[] = {1, 5, 4, 16, 10}
Output: 15
方法:初始化变量sum = 0,并使用Brian Kernighan算法对数组进行从0到n – 1的遍历,同时计算arr [i]中设置的位数。如果计数为偶数,则更新sum = sum + arr [i] 。最后打印总和。
下面是上述方法的实现:
C++
// C++ program to find the sum of the elements
// from an array which have even parity
#include
using namespace std;
// Function that returns true if x has even parity
bool checkEvenParity(int x)
{
// We basically count set bits
// https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
int parity = 0;
while (x != 0) {
x = x & (x - 1);
parity++;
}
if (parity % 2 == 0)
return true;
else
return false;
}
// Function to return the sum of the elements
// from an array which have even parity
long sumlist(int a[], int n)
{
long sum = 0;
for (int i = 0; i < n; i++) {
// If a[i] has even parity
if (checkEvenParity(a[i]))
sum += a[i];
}
return sum;
}
// Driver code
int main()
{
int arr[] = { 2, 4, 3, 5, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << sumlist(arr, n);
return 0;
}
Java
// Java program to find the sum of the elements
// from an array which have even parity
import java.io.*;
class GFG {
// Function that returns true if x has even parity
static boolean checkEvenParity(int x)
{
// We basically count set bits
// https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
int parity = 0;
while (x != 0) {
x = x & (x - 1);
parity++;
}
if (parity % 2 == 0)
return true;
else
return false;
}
// Function to return the sum of the elements
// from an array which have even parity
static long sumlist(int a[], int n)
{
long sum = 0;
for (int i = 0; i < n; i++) {
// If a[i] has even parity
if (checkEvenParity(a[i]))
sum += a[i];
}
return sum;
}
// Driver code
public static void main (String[] args) {
int arr[] = { 2, 4, 3, 5, 9 };
int n =arr.length;
System.out.println(sumlist(arr, n));
}
}
// This code is contributed by inder_verma..
Python3
# Python3 program to find the sum of the elements
# from an array which have even parity
# Function that returns true if x
# has even parity
def checkEvenParity(x):
# We basically count set bits
# https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
parity = 0
while (x != 0):
x = x & (x - 1)
parity += 1
if (parity % 2 == 0):
return True
else:
return False
# Function to return the sum of the elements
# from an array which have even parity
def sumlist(a, n):
sum = 0
for i in range(n):
# If a[i] has even parity
if (checkEvenParity(a[i])):
sum += a[i]
return sum
# Driver code
if __name__ == '__main__':
arr = [ 2, 4, 3, 5, 9 ]
n = len(arr)
print(sumlist(arr, n))
# This code is contributed by 29AjayKumar.
C#
// C# program to find the sum of the elements
// from an array which have even parity
using System ;
class GFG {
// Function that returns true if x has even parity
static bool checkEvenParity(int x)
{
// We basically count set bits
// https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
int parity = 0;
while (x != 0) {
x = x & (x - 1);
parity++;
}
if (parity % 2 == 0)
return true;
else
return false;
}
// Function to return the sum of the elements
// from an array which have even parity
static long sumlist(int []a, int n)
{
long sum = 0;
for (int i = 0; i < n; i++) {
// If a[i] has even parity
if (checkEvenParity(a[i]))
sum += a[i];
}
return sum;
}
// Driver code
public static void Main () {
int []arr = { 2, 4, 3, 5, 9 };
int n =arr.Length;
Console.WriteLine(sumlist(arr, n));
}
// This code is contributed by Ryuga
}
PHP
Javascript
输出:
17