hdu1003
自己粗心大意了啊,结果没有通过
#include "iostream" #include "string.h" using namespace std; int maxb(int a,int b){return a>b?a:b;} int f[10000][10000]; int main(){ int Case,i,num,j,list[10010],sum,max,k,s1,s,e,min,v,m; cin>>Case; for(m=1;m<=Case;m++){ cin>>num; for(j=1;j<=num;j++)cin>>list[j]; max=list[1];s=1;e=1; for(i=1;i<=num;i++){ sum=0; //cout<<i<<" "; for(j=i;j<=num;j++){ sum+=list[j]; //cout<<sum<<' '; if(sum>max){max=sum;s=i;e=j;} } //cout<<endl; } /*sum=list[1];max=list[1];min=list[1];s=1;s1=1; for(k=2;k<=num;k++){ if(sum<0){min=sum;sum=list[k];s1=k;} else sum+=list[k]; if(sum>max){max=sum;s=s1;e=k;} }*/ /*max=list[1];s=1;e=1; for(v=1;v<=num;v++)f[v][v]=list[v]; for(v=1;v<num;v++){ for(i=1;i<=num-v;i++){ j=i+v; f[i][j]=f[i][j-1]+list[j]; if(max<f[i][j]){max=f[i][j];s=i,e=j;} } }*/ cout<<"Case "<<m<<":"<<endl; cout<<max<<' '<<s<<' '<<e<<endl; } }
贴一下网上的代码
#include"iostream" using namespace std; int main() { int t; int n,a; int i,j=0,k; cin>>t; while(t--) { cin>>n; int sum=0,Max=-99999999; int start=0,end=0; k=0; for(i=0;i<n;i++) { cin>>a; sum+=a; if(sum>Max) { Max=sum; start=k+1; end=i+1; } if(sum<0) { sum=0; k=i+1; } } cout<<"Case "<<++j<<":"<<endl; cout<<Max<<" "<<start<<" "<<end<<endl; if(t>0) cout<<endl; } return 0; }