HDU 1231最大连续子序列

HDU 1231最大连续子序列
 http://acm.hdu.edu.cn/showproblem.php?pid=1231

求最大连续子序列,并记录首尾元素。
#include  < iostream >

using   namespace  std;

int  a[ 10000 ];

int  main()
{
    
int k,i,temp,sum,begin,end,tb;
    
while(scanf("%d",&k),k)
    
{
        
for(i=0;i<k;i++)
            scanf(
"%d",&a[i]);
        temp
=begin=end=tb=0;
        sum
=-1;
        
for(i=0;i<k;i++)
        
{
            temp
+=a[i];
            
if(temp>sum)
            
{
                sum
=temp;
                end
=i;
                begin
=tb;
            }

            
if(temp<0)
            
{
                temp
=0;
                tb
=i+1;
            }

        }

        
if(sum==-1)   //若sum==-1,则输入数值全是负值
        
{
            sum
=0;
            begin
=0;
            end
=k-1;
        }

        printf(
"%d %d %d\n",sum,a[begin],a[end]);
    }

    
return 0;
}

/*

Sample Input

6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0

Sample Output

20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
*/


你可能感兴趣的:(HDU 1231最大连续子序列)