标签:blog io for 2014 log amp as size new
#include <stdio.h> #include <stdlib.h> int main() { int this_sum, max_sum, old_first, old_last, new_first; int n,i, tmp, flag = 1; int first = 1; int *data; scanf("%d", &n); this_sum= max_sum = old_first = new_first = 0; old_last = n - 1; data = (int *)malloc(n * sizeof(int)); for(i = 0; i < n; i ++) { scanf("%d", &data[i]); } for(i = 0; i < n; i++) { this_sum += data[i];/*向右累加*/ if(first)//第一次找到最大的和 { if(this_sum >= 0) { first = 0; old_first = old_last = i; } } if(flag)//获取新的this_sum的开头 { if(this_sum >= 0) { flag = 0; new_first = i; } } if(this_sum > max_sum) { max_sum = this_sum;/*发现更大的和,则更新当前结果*/ old_last = i; if(new_first != old_first) { old_first = new_first; } } else if(this_sum < 0)/*如果当前子序列和为负*/ { this_sum = 0;/*则不可能使后面的和增大,抛弃之*/ flag = 1; } } printf("%d %d %d\n", max_sum, data[old_first], data[old_last]); return 0; }
标签:blog io for 2014 log amp as size new
原文地址:http://blog.csdn.net/it_liuwei/article/details/41991839