阵列中连续元素的总和C
Sum of consecutive elements in an array, C++
假设我有一个由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;
}
这也应该使用先前计算的总和。
- 我应该如何表示我拥有的连续元素序列?
- 如何将 n 个连续元素插入到元素类型不可复制的 std::vector 中?
- 是否存在一个范围::视图::group_by对应项,它将所有元素都考虑在内,而不是只考虑连续的元素
- 结构化绑定,无需复制即可获取子向量的连续元素
- 确定数组是否可以旋转 3 个连续的数组元素进行排序?
- C++指向连续矢量元素的指针是矛盾的
- 减去连续的集元素
- 如何比较C 列表中的两个连续元素
- 连续单个元素的敏感变量
- 将解决方案打印为数组中非连续元素的最大总和
- AVX2 根据条件将连续元素扩展到稀疏向量?(如AVX512 VP扩展)
- 我如何连续地找到“占领”C 数组中的元素
- 是否有任何方法可以比较C 中的STD ::的两个连续元素
- 在C 中添加连续的向量元素组的最快方法
- 如何在c++中增加向量中k个连续元素的值
- C++用于将函数应用于连续元素的算法
- 阵列中连续元素的总和C
- 一种按排序顺序保持元素的数据结构,支持快速插入和计算连续元素之间的最大差异
- 返回连续数字的逻辑假设数组包含元素 1,9,2,3,4,9,6,7,8,那么它应该返回 2,3,4,6,7,8
- 从数组中每 3 个连续选择元素并使用函数进行比较