【发布时间】:2023-03-31 11:51:02
【问题描述】:
我的代码说明:
例如,取n = 3,让p [i] 为1 2 3。首先,max = 0。我按以下顺序排列序列:1, 1 + 2, 1 + 2 + 3, 2, 2 + 3, 3,每次找到某种数量时,我都会将其与最大数量进行比较。
我的代码是这样的:
#include <iostream>
using namespace std;
//f - number of all sequences(sequence length 1 to n)
//n - number of p[i]
//q is sequence sum
//x this is the number that increases when we get to the last element. For example, the numbers 1 2 3, when we reach 1 + 2 + 3, then x grows and go to 2, 2 + 3
int main()
{
int n, i, f = 0, q, x = 0, max, j;
cin >> n;
int *p = new int[n]();
for (i = 0; i < n; i++, f += i) {
cin >> p[i];
}
max = p[0];
for (i = 0 ; i < f; x++ ) {
q = 0;
for (j = x; j < n; j++,i++) {
q += p[j];
if (q >= max) {
max = q;
}
}
}
cout <<max;
return 0;
}
这是 90% 的结果:
【问题讨论】:
-
这些类型的测试旨在检查您是否处理了所有的边缘情况。失败的可能是由于特殊情况,而不是表示通常不正确的算法。您需要一遍又一遍地阅读规范,直到您了解哪些可能的意外输入将被视为有效并对其进行测试。
-
请重命名您的变量,以便理解它们应该做什么。例如,不清楚
f的用途。 -
@kiner_shah f - 所有序列的数量(序列长度 1 - n)
-
@WithOrxan,请更新您的代码并将所有变量名替换为有意义的名称。
-
您需要考虑的一点:如果奶牛的最大利润实际上是亏损怎么办?