HDU1231
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 最大连续子序列和 3 输出 ans 和 首元素,尾元素 4 */ 5 6 /* 7 //TLE 只适合处理小型数据 8 #include<stdio.h> 9 #include<string.h> 10 #include<stdlib.h> 11 #include<algorithm> 12 #include<iostream> 13 #include<queue> 14 using namespace std; 15 const int maxn = 10005; 16 int a[ maxn ]; 17 int main(){ 18 int n; 19 while( scanf("%d",&n) !=EOF ){ 20 if( n==0 ) break; 21 for( int i=1;i<=n;i++ ) 22 scanf("%d",&a[ i ]); 23 int st=1,en=1; 24 int ans=0; 25 int flag=-1; 26 for( int i=1;i<=n;i++ ){ 27 int tmp=0; 28 for( int j=i;j<=n;j++ ){ 29 tmp+=a[ j ]; 30 if( tmp>ans ){ 31 ans=tmp; 32 st= i ; 33 en= j ; 34 flag=1; 35 continue; 36 } 37 if( ans==tmp ){ 38 if(flag==-1){ 39 st=i; 40 en=j; 41 flag=1; 42 } 43 } 44 } 45 } 46 if( flag==-1 ) 47 printf("0 %d %d\n",a[1],a[n]); 48 else{ 49 printf("%d %d %d\n",ans,a[st],a[en]); 50 } 51 } 52 return 0; 53 } 54 */ 55 #include<stdio.h> 56 #include<string.h> 57 #include<stdlib.h> 58 #include<algorithm> 59 #include<iostream> 60 #include<queue> 61 using namespace std; 62 const int maxn = 10005; 63 int a[ maxn ]; 64 int sum[ maxn ]; 65 int main(){ 66 int n; 67 while( scanf("%d",&n)!=EOF && n ){ 68 for( int i=1;i<=n;i++ ) 69 scanf("%d",&a[ i ]); 70 int ans;//-1 71 memset( sum,0,sizeof(sum) ); 72 int st,en,x,y; 73 ans=st=en=x=y=a[ 1 ];//x,y在这里能初始化 74 sum[ 1 ]=a[ 1 ]; 75 for( int i=2;i<=n;i++ ){ 76 if( sum[ i-1 ]<=0 ){ 77 sum[ i ]=a[ i ]; 78 st=en=a[ i ]; 79 } 80 else{ 81 sum[ i ]=sum[ i-1 ]+a[ i ]; 82 en=a[ i ]; 83 } 84 if( ans<sum[ i ] ){ 85 ans=sum[ i ]; 86 x=st; 87 y=en; 88 } 89 } 90 if( ans<0 ){ 91 printf("0 %d %d\n",a[1],a[n]); 92 } 93 else{ 94 printf("%d %d %d\n",ans,x,y); 95 } 96 } 97 return 0; 98 }
keep moving...