标签:
Description
Input
Output
Sample Input
Sample Output
#include <cstdio> int main() { int T,k, R, L, Max, sum, t, total=1, n; scanf("%d", &T); while(T--) { sum = Max =-100; //足够小就好 scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%d", &t); if (sum<0) { sum = t, k = i; } else sum += t; if (Max < sum) Max = sum, L = k, R = i; //L R分别为初始和结束位置 } printf("Case %d:\n", total++); printf("%d %d %d\n", Max, L, R ); if (T) printf("\n"); } return 0; }
#include<iostream> using namespace std; int a[100010],b[100010]; int main() { int n,T,s,t,max,total,k=1; cin>>T; while(T--) { cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; b[1]=a[1]; for(int i=2; i<=n; i++) { if(b[i-1]<0) b[i]=a[i]; else b[i]=b[i-1]+a[i]; } max=b[1]; s=1; for(int i=2; i<=n; i++) { if(b[i]>max) { max=b[i]; s=i; } } t=s; total=0; for(int i=s; i>=1; i--) { total+=a[i]; if(total==max) t=i; } cout<<"Case "<<k++<<":"<<endl; cout<<max<<" "<<t<<" "<<s<<endl; if(T) cout<<endl; } }
标签:
原文地址:http://www.cnblogs.com/hfc-xx/p/4719229.html