c++ - 2d Kadane 算法中的逻辑错误有什么问题?
问题描述
我正在实现一个程序,该程序使用 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;
}
解决方案
推荐阅读
- imap - 高效查找 IMAP 中每个文件夹的起始 UID
- node.js - QNA Maker 不返回完全匹配
- path - 为什么我不能用应用程序的路径启动 celery
- reactjs - 在反应中更改动态创建元素的样式
- sql-server - 将现有的基于磁盘的表迁移到内存优化表
- python - python-pptx / 更新现有的 Powerpoint 图表而不替换其 excel 工作表
- html - 布尔玛导航响应关闭
- sql - 在连接中使用派生表 (Teradata)
- javascript - 循环中的 JSX 条件元素
- winforms - Windows.Form Combobox 与较大显示器中的下拉列表不同步