阵列中连续元素的总和C

Sum of consecutive elements in an array, C++

本文关键字:元素 连续 阵列      更新时间:2023-10-16

假设我有一个由n个元素组成的数组。

1 2 3 4 5 6 ... n

我需要找到一种使用C 在此数组中提取连续元素的总和。这样:

1, 2, 3,...n, 1+2, 2+3, 3+4,...(n-1)+n, 1+2+3, 2+3+4,...(n-2)+(n-1)+n,...1+2+3...n

到目前为止,我发现我需要通过将每次运行中的一定数量的元素求和来迭代此数组。我不确定是否可以实现上面解释的算法。可能有一个更好的解决方案,但这是我能想到的最好的。

您可以使用std::transform来做到这一点:

std::transform(
    v.begin(), v.end()-1,
    v.begin()+1,
    std::ostream_iterator<int>(std::cout, "n"),
    std::plus<int>()
);

当然,您不必在输出时使用Ostream_iterator,也可以使用其他容器迭代器或std::back_inserter用于容器或任何其他OutputIterator

参考

http://en.cppreference.com/w/cpp/algorithm/transform

http://en.cppreference.com/w/cpp/utility/functional/plus

http://en.cppreference.com/w/cpp/container/vector

编辑:

std::vector<int> v(100), t;
//this just populates v with 1,2,3...100
std::iota(v.begin(), v.end(), 1);
std::transform(
    v.begin(), v.end()-1, v.begin()+1,
    std::back_inserter(t),
    std::plus<int>()
);
std::transform(
    t.begin(), t.end()-1, v.begin()+2,                
    std::ostream_iterator<int>(std::cout, "n"),
    std::plus<int>()
);

让我们用4个元素检查案例:

{1,3,4,5, // from original array
 4,7,9, // sum of 2 consecutive elements
 8,12, // sum of 3
 13} // sum of 4

您可以看到(n-1)的原始数组的每个零件的大小较小。因此,您需要大小的目标阵列:n (n-1) (n-2) ... 1-是n*(1 n)/2

int* createSumArray(int* arr, int size)
{
   int ti = 0; // target index
   int* ta = new int[size*(size+1)/2];
   for (int s = 1; s <= size; ++s) // how many elements to sum
   {
      for (int si = 0; si < size + 1 - s; ++si)
      {
          ta[ti] = 0;
          for (int i = si; i < si + s; ++i)
            ta[ti] += arr[i];
          ++ti;
      } 
   }
   return ta;
}

请参阅IDEONE上的测试

如何。给定5个整数的数组:5、7、3、9、4


    void DoMaths (void)
    {
         int       iArray [] = { 5, 7, 3, 9, 4 } ;
         int       iSize = 5 ;
    
         int       iGroup ;
         int       iIndex ;
         int       iPass ;
         int       iResults ;
         int       iStart ;
         int       iSum ;
    
    // Init
         iGroup   = 1 ;
         iResults = iSize ;
    // Repeat for each pass
         for (iPass = 0 ; iPass < iSize ; iPass ++)
         {
              printf ("n") ;
              printf ("Pass %d : Group %d :n", iPass, iGroup) ;
         // Repeat for each group of integers in a pass
              for (iStart = 0 ; iStart < iResults ; iStart ++)
              {
                   iSum = 0 ;
                   printf ("  %d [ ", iStart) ;
                   for (iIndex = iStart ; iIndex < (iStart + iGroup) ; iIndex ++)
                   {
                        printf ("%d ", iIndex) ;
                        iSum += iArray [iIndex] ;
                   }
                   printf ("] sum = %d n", iSum) ;
              }
              iGroup ++ ;
              iResults -- ;
         }
         return ;
    }

这产生以下结果...

    通过0:第1组:      0 [0] sum = 5      1 [1] sum = 7      2 [2] sum = 3      3 [3] sum = 9      4 [4] sum = 4        通过1:第2组:      0 [0 1] sum = 12      1 [1 2] sum = 10      2 [2 3]总和= 12      3 [3 4]总和= 13        通过2:第3组:      0 [0 1 2] sum = 15      1 [1 2 3] sum = 19      2 [2 3 4] sum = 16        通过3:第4组:      0 [0 1 2 3]总和= 24      1 [1 2 3 4] sum = 23        通过4:第5组:      0 [0 1 2 3 4] sum = 28
int main()
{
    int ptr=0,i,j,k;
    int Ar[]={1,2,3,4,5,6,7,8,9,10,11,12,13};
    int n=13;
    int *Res;
    Res=(int*)calloc(n*(n+1)/2,sizeof(int));
    for(i=1;i<=n;i++) //tells about how many element's sum we need
    for(j=i;j<=n;j++)
    {
        for(k=0;k<i;k++)
        {      
               Res[ptr]+=Ar[j-i+k];
        }
        ptr++;
    }
    for(int x=0;x<ptr;x++)
    cout<<Res[x]<<"t";
    return 0;
}

让我们调用原始数组a。

让我们称之为k连续元素的总和b。

让我们调用K 1连续元素的总和c。

每个数组的大小n。

C的第一个K-2细胞无关紧要。

for(int i = k-1; i < n; i++)
    C[i] = A[i-1] + B[i];

迭代每个k的上述代码,最多n,然后在每次通过后,将结果数组与上一个迭代的结果相连。(确保很好地检查角案例)

请参见在ideone.com上工作:

std::vector<std::vector<int> > sums(int array[], int size)
{
        std::vector<std::vector<int> > result(size - 1);
        //build the two element sums
        for(int *p = array; p - array < size - 1; ++p)
                result[0].push_back(std::accumulate(p, p + 2, 0));
        //build the rest of the sums
        for(int i = 1; i < size - 1; ++i)
                for(int j = 0; j < size - (i + 1); ++j)
                        result[i].push_back(result[i - 1][j] + array[i + j + 1]);
        return result;
}

这也应该使用先前计算的总和。