WA了好几次,原因是开始写的是
int t, n, sum, max, start, end, i, j, temp;
int flag=0;
改成这样就AC了:
int t, n, sum, max, start, end, i, j, temp, flag;
#include <stdio.h> int main() { int t, n, sum, max, start, end, i, j, temp, flag; //flag : flag the start position scanf("%d", &t); for(j=0; j<t; j++) { scanf("%d", &n); sum = 0; start = 0; end = 0; flag = 0; max = -1001;// because all the integers are between -1000 and 1000 for(i=0; i<n; i++) { scanf("%d", &temp); sum += temp; if(max < sum) { max = sum; start = flag; end = i; } if(sum < 0) { flag = i + 1; sum = 0; } } printf("Case %d:\n%d %d %d\n", j+1, max, start+1, end+1); if(j<t-1) printf("\n"); } return 0; }