我正在实现一个程序,该程序使用 kadane 算法计算二维数组的最大小计。但是,我得到了逻辑错误。我想我在 for 循环中犯了错误,但我找不到它。
这是我的代码有问题的部分。
void kadane2D(double array[][1000], int n)
{
for (int i = 1; i < n; i++)
for (int j = 0; j < n; j++)
array[i][j] += array[i-1][j];
double sums[n];
double ans = sums[0];
double a = sums[0];
for(int top=1; top<n; top++){
for(int bottom=top; bottom<n; bottom++)
{
for(int i=0; i<n; i++)
sums[i] = array[bottom][i] - array[top-1][i];
a = min(a, min_kadane1d(sums, n));
ans = max(ans, kadane1d(sums, n));
}
}
cout << a << endl;
cout << ans << endl;
}