#include
#include
using namespace std;
int panduan(int a[],int b,int N)//返回一个负数向后多少位能加成正数
{
int temp=0,bb=b;
do{temp+=a[bb];bb++;}
while(temp<0&&bb
else{return -1;}
}
int qiuzhi(int a[],int &shouwei,int &weishu,int N)
{
int t=0,sum=0;
while(a[t]<0&&t
{
sum=a[t];
for(int i=0;i
if(a[i]>sum)
sum=a[i];
}
weishu=1;
}
else//如果不都是负数
{
int pp=t,oo;
do
{ t=0;
if(a[pp]>=0){sum+=a[pp];pp++;}
else
{
oo=panduan(a,pp,N);//从panduan函数返回的负数后面的位
if(oo!=-1)
{do{sum+=a[pp+t];t++;}//后面如果不是负的就加上
while(t<=oo);
pp=pp+t;}
else
break;//如果是负的,就这样的
}
}while(pp
}
return sum;
}
void main()
{
ifstream input("data.txt");
int T,shouwei,weishu,ww=1;
long N,sum1;
input>>T;
do//每行一次循环
{
int sw,ws;
sum1=0;
input>>N;
int *a=new int[N];
for(int j=0;j
cout<<"case "<
ws=weishu;sw=shouwei;
for(int u=0;u
if(qiuzhi(a+u,shouwei,weishu,N-u)>sum1)
{sum1=qiuzhi(a+u,shouwei,weishu,N-u);
ws=weishu;sw=shouwei;}
}
cout<
}
while(ww<=T);
cin>>N;
}
#include
main()
{ int i,j,n,a,t,x,y,m,max,k,l;
while(scanf("%d",&n)!=EOF)
{
for (i=0;i
scanf("%d",&m);
max=-9999;t=0;k=0;l=1;
for(j=1;j<=m;j++)
{
scanf("%d",&a);
t=t+a;
if (t>max)
{
max=t;
y=l;
x=j;
}
if (t<0)
{
l=j+1;
t=0;
}
}
printf("Case %d:\n",i+1);
printf("%d %d %d\n",max,y,x);
if(i!=n-1)
printf("\n");
}
}
}
动态规划