标签:代码 ber strong open turn out style code nbsp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 252943 Accepted Submission(s): 60001
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 int main() 7 { 8 //freopen("in.txt","r",stdin); 9 int T, n, subsum, ans; //subsum:当前部分和,ans:答案 10 int N = 1; 11 int Sta, End, Sta1; //Sta:至今最大和起始位置,End:至今最大和终止位置,Sta1当前部分和的起始位置 12 int a[110000]; 13 scanf("%d",&T); 14 while( T--) { 15 scanf("%d",&n); 16 if(N != 1) printf("\n"); 17 printf("Case %d:\n",N++); 18 ans = -1, Sta = End = Sta1 = 1, subsum = 0; 19 for(int i = 1 ; i <= n ; i++ ) { 20 scanf("%d",&a[i]); 21 subsum += a[i]; 22 if(subsum < 0) {Sta1 = i+1 ; subsum = 0;}//当前部分和是负数,则遗弃当前段部分和,寻找下一段 23 else { 24 if(subsum > ans) {//当前部分和大于之前的某一段部分和,则更新 25 Sta = Sta1; 26 End = i; 27 ans = subsum ; 28 } 29 } 30 31 } 32 if(ans == -1) {//判断全是负数的情况 33 ans = -100000; 34 for(int i = 1; i <= n; i++) { 35 if(ans < a[i]) { 36 ans = a[i]; 37 Sta = End = i; 38 } 39 } 40 printf("%d %d %d\n",ans ,Sta, End); 41 } 42 else 43 printf("%d %d %d\n",ans, Sta, End); 44 } 45 return 0; 46 }
标签:代码 ber strong open turn out style code nbsp
原文地址:http://www.cnblogs.com/creativepower/p/7306158.html