在阵列中,C的连续元素总和++(Sum of consecutive elements in an

2019-10-17 08:40发布

假设我具有由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

到目前为止,我想通了,我需要通过总结若干各运行要素通过这个数组进行迭代。 我不知道是否有可能实现我上面所解释的算法。 有可能是一个更好的解决方案,但是,这是我能拿出最好的。

Answer 1:

让我们检查的情况下有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求和阵列每一个部分是大小为下从原始阵列通过(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



Answer 2:

您可以使用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>()
);


Answer 3:

如何。 给予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 ;
    }

这将产生以下结果...


    Pass 0 : Group 1 :
      0 [ 0 ] sum = 5
      1 [ 1 ] sum = 7
      2 [ 2 ] sum = 3
      3 [ 3 ] sum = 9
      4 [ 4 ] sum = 4

    Pass 1 : Group 2 :
      0 [ 0 1 ] sum = 12
      1 [ 1 2 ] sum = 10
      2 [ 2 3 ] sum = 12
      3 [ 3 4 ] sum = 13

    Pass 2 : Group 3 :
      0 [ 0 1 2 ] sum = 15
      1 [ 1 2 3 ] sum = 19
      2 [ 2 3 4 ] sum = 16

    Pass 3 : Group 4 :
      0 [ 0 1 2 3 ] sum = 24
      1 [ 1 2 3 4 ] sum = 23

    Pass 4 : Group 5 :
      0 [ 0 1 2 3 4 ] sum = 28

我希望这有帮助...



Answer 4:

我觉得这个代码应该做你所要求的:

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;
}


Answer 5:

让我们把原来的阵列A.

让我们把k个连续元素B.资金的阵列

让我们称之为+ 1个连续元素C.中的k总和的阵列

每个阵列的是大小为n的。

前k-2 C的细胞是不相关的。

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

迭代上述各码k到n和各后通过串联所得阵列以从先前的迭代的结果。 (请务必检查角落案件井)



Answer 6:

看到它的工作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;
}

这应该使用先前计算的数额了。



文章来源: Sum of consecutive elements in an array, C++