fork(2) download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <algorithm>
  4.  
  5. int* createSumArray(int* arr, int size)
  6. {
  7. int ti = 0; // target index
  8. int* ta = new int[size*(size+1)/2];
  9. for (int s = 1; s <= size; ++s) // how many elements to sum
  10. {
  11. for (int si = 0; si < size + 1 - s; ++si)
  12. {
  13. ta[ti] = 0;
  14. for (int i = si; i < si + s; ++i)
  15. ta[ti] += arr[i];
  16. ++ti;
  17. }
  18. }
  19. return ta;
  20. }
  21.  
  22. void test(int* a, size_t N)
  23. {
  24. std::copy(a, a + N, std::ostream_iterator<int>(std::cout, ","));
  25. std::cout << "\n";
  26. int* ret = createSumArray(a, N);
  27. std::copy(ret, ret + N*(N+1)/2, std::ostream_iterator<int>(std::cout, ","));
  28. std::cout << "\n";
  29. delete [] ret;
  30. }
  31.  
  32. int main() {
  33. {
  34. int a[] = {1};
  35. test(a, sizeof(a)/sizeof(*a));
  36. }
  37. {
  38. int a[] = {1,3,4,5};
  39. test(a, sizeof(a)/sizeof(*a));
  40. }
  41. {
  42. int a[] = {1,2,3,4,5,6,7,8,9,10};
  43. test(a, sizeof(a)/sizeof(*a));
  44. }
  45. }
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
1,
1,
1,3,4,5,
1,3,4,5,4,7,9,8,12,13,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,3,5,7,9,11,13,15,17,19,6,9,12,15,18,21,24,27,10,14,18,22,26,30,34,15,20,25,30,35,40,21,27,33,39,45,28,35,42,49,36,44,52,45,54,55,