📜  所有子数组的按位或的总和

📅  最后修改于: 2021-05-25 05:27:14             🧑  作者: Mango

给定一个正整数数组,在给定数组的所有子数组上执行按位或运算后,找到总和。

例子:

Input : 1 2 3 4 5
Output : 71

Input : 6 5 4 3 2
Output : 84

首先初始化两个变量sum = 0,sum1 = 0,变量sum将存储总和,并使用sum1对每个第j个元素执行按位或运算,然后将sum1与sum相加。
1:-从第0个位置移动到n-1。
2:对于每个ith变量,我们将对所有子数组执行按位或运算,以找到总和。
重复步骤,直到遍历整个数组。

C++
// C++ program to find sum of 
// bitwise ors of all subarrays.
#include 
using namespace std;
  
int totalSum(int a[], int n)
{
    int i, sum = 0, sum1 = 0, j;
  
    for (i = 0; i < n; i++)
    {
  
        sum1 = 0;
  
        // perform Bitwise OR operation
        // on all the subarray present 
        // in array
        for (j = i; j < n; j++) 
        {
  
            // OR operation
            sum1 = (sum1 | a[j]);
  
            // now add the sum after performing 
            // the Bitwise OR operation
            sum = sum + sum1;
        }
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << totalSum(a, n) << endl;
    return 0;
}
  
// This code is contributed
// by Shivi_Aggarwal


C
// C program to find sum of bitwise ors
// of all subarrays.
#include 
  
int totalSum(int a[], int n)
{
    int i, sum = 0, sum1 = 0, j;
  
    for (i = 0; i < n; i++) {
  
        sum1 = 0;
  
        // perform Bitwise OR operation
        // on all the subarray present in array
        for (j = i; j < n; j++) {
  
            // OR operation
            sum1 = (sum1 | a[j]);
  
            // now add the sum after performing the
            // Bitwise OR operation
            sum = sum + sum1;
        }
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(a)/sizeof(a[0]);
    printf("%d ", totalSum(a, n));
    return 0;
}


Java
// Java program to find sum 
// of bitwise ors of all subarrays.
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
static int totalSum(int a[], int n)
{
    int i, sum = 0, sum1 = 0, j;
  
    for (i = 0; i < n; i++) 
    {
        sum1 = 0;
  
        // perform Bitwise OR operation
        // on all the subarray present 
        // in array
        for (j = i; j < n; j++) 
        {
  
            // OR operation
            sum1 = (sum1 | a[j]);
  
            // now add the sum after 
            // performing the Bitwise
            // OR operation
            sum = sum + sum1;
        }
    }
  
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    int a[] = { 1, 2, 3, 4, 5 };
    int n = a.length;
    System.out.println(totalSum(a,n));
}
}
  
// This code is contributed 
// by Subhadeep


Python3
# Python3 program to find sum of
# bitwise ors of all subarrays.
def totalSum(a, n):
    sum = 0;
    for i in range(n):
        sum1 = 0;
          
        # perform Bitwise OR operation
        # on all the subarray present
        # in array
        for j in range(i, n):
              
            # OR operation
            sum1 = (sum1 | a[j]);
              
            # now add the sum after
            # performing the
            # Bitwise OR operation
            sum = sum + sum1;
    return sum;
  
# Driver code
a = [1, 2, 3, 4, 5];
n = len(a);
print(totalSum(a, n));
  
# This code is contributed by mits


C#
// C# program to find sum 
// of bitwise ors of all 
// subarrays.
using System;
  
class GFG
{
static int totalSum(int[] a, int n)
{
    int sum = 0;
    for(int i = 0; i < n; i++) 
    {
        int sum1 = 0;
  
        // perform Bitwise OR operation
        // on all the subarray present 
        // in array
        for (int j = i; j < n; j++) 
        {
  
            // OR operation
            sum1 = (sum1 | a[j]);
  
            // now add the sum after 
            // performing the Bitwise
            // OR operation
            sum = sum + sum1;
        }
    }
  
    return sum;
}
  
// Driver code
static void Main()
{
    int[] a = { 1, 2, 3, 4, 5 };
    int n = a.Length;
    Console.WriteLine(totalSum(a,n));
}
}
  
// This code is contributed 
// by mits


PHP


输出:
71