1003 郁闷了一下午的题
看题不仔细,没考虑所有数字都可能小于0的情况,都被DS书上的例子给影响了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int res[110000];
int main()
{
int cnt, n;
int thisSum, maxSum;
int i, j;
int tag;
int max;
int sta, msta, lst, mlst;
scanf("%d", &cnt);
for (i = 1; i <= cnt; ++i) {
printf("Case %d:\n", i);
scanf("%d", &n);
tag = 1;
for (j = 1; j <= n; ++j) {
scanf("%d", &res[j]);
if (res[j] >= 0) tag = 0;
}
if (tag) {
max = 1;
for (j = 2; j <= n; ++j) {
if (res[j] > res[max]) max = j;
}
printf("%d %d %d\n", res[max], max, max);
}
else {
for (j = 1; j <= n; ++j) {
if (res[j] >= 0) break;
}
thisSum = maxSum = res[j];
sta = lst = j;
msta = mlst = j;
for (j = j+1; j <= n; ++j) {
thisSum += res[j];
lst = j;
if (thisSum > maxSum) {
maxSum = thisSum;
msta = sta; mlst = lst;
} else if (thisSum < 0) {
thisSum = 0;
sta = lst = j+1;
}
}
printf("%d %d %d\n", maxSum, msta, mlst);
}
if (i != cnt) printf("\n");
}
system("pause");
return 0;
}


浙公网安备 33010602011771号