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
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0Huge input, scanf is recommended.HintHint
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int a[10001]; int dp[10001]; int s[100001]; int main() { int k,max,start,end,flag; while(cin>>k,k) { flag=0; memset(dp,0,sizeof(dp)); memset(a,0,sizeof(a)); //memset(s,0,sizeof(s)); s[0]=s[1]=1; for(int i=1;i<=k;i++) { cin>>a[i]; if(a[i]>=0)flag=1; } if(flag==1) { for(int i=1;i<=k;i++) { if(dp[i-1]+a[i]>a[i]) { dp[i]=dp[i-1]+a[i]; s[i]=s[i-1]; } else { dp[i]=a[i]; s[i]=i; } } max=dp[1];start=1;end=1; for(int i=2;i<=k;i++) { if(max<dp[i]) { max=dp[i]; start=s[i]; end=i; } } cout<<max<<" "<<a[start]<<" "<<a[end]<<endl; } else { cout<<"0"<<" "<<a[1]<<" "<<a[k]<<endl; } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/it142546355/article/details/47340301