太久没做题了,手都生了不少,所以从比较简单的题目开始做起。先上题目
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 123917 Accepted
Submission(s): 28659
1 #include <cstdio> 2 #include <cstring> 3 #define MAX 100001 4 using namespace std; 5 6 int a[MAX]; 7 int n; 8 9 int main() 10 { 11 int t,pos,st,ed,maxn,sum; 12 scanf("%d",&t); 13 for(int i=1;i<=t;i++){ 14 scanf("%d",&n); 15 for(int j=0;j<n;j++){ 16 scanf("%d",&a[j]); 17 } 18 ed=st=pos=0; 19 maxn=sum=a[0]; 20 for(int j=1;j<n;j++){ 21 if(sum+a[j]<a[j]){ //加上当前位置的数以后最大值会变小,或者是前面的当前最大值是负数 22 pos=j; 23 sum=a[j]; 24 }else{ //序列还在上升,所以暂定最大值继续累加。 25 sum+=a[j]; 26 } 27 if(sum>maxn){ //判定暂定的最大值是不是全体最大值,如果是就将全体最大值替换 28 maxn=sum; 29 st=pos; 30 ed=j; 31 } 32 } 33 printf("Case %d:\n%d %d %d\n",i,maxn,st+1,ed+1); 34 if(i!=t) printf("\n"); 35 } 36 return 0; 37 }
原文:http://www.cnblogs.com/sineatos/p/3515309.html