在C ++中反复连接后创建的数组中的最大子数组总和
在本教程中,我们将讨论在重复连接后创建的数组中查找最大子数组和的程序。
为此,我们将提供一个数组和一个整数K。我们的任务是在给定数组重复K次后,找到具有最大元素数的子数组。
示例
#include<bits/stdc++.h>
using namespace std;
//返回最大子数组的总和
int maxSubArraySumRepeated(int a[], int n, int k) {
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < n*k; i++) {
max_ending_here = max_ending_here + a[i%n];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0) max_ending_here = 0;
}
return max_so_far;
}
int main() {
int a[] = {10, 20, -30, -1};
int n = sizeof(a)/sizeof(a[0]);
int k = 3;
cout << "Maximum contiguous sum is "
<< maxSubArraySumRepeated(a, n, k);
return 0;
}输出结果
Maximum contiguous sum is 30
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练