杭电ACM1003 WA~~~ 大虾帮下小弟

2024-11-25 23:40:50
推荐回答(3个)
回答1:

#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&&bbif(temp>=0){return bb-b-1;}
else{return -1;}
}

int qiuzhi(int a[],int &shouwei,int &weishu,int N)
{
int t=0,sum=0;
while(a[t]<0&&tif(a[t]<0)//说这一行的数组全是负数,直接找到一个最大值就行了
{
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(ppweishu=pp-shouwei+1;
}
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 {input>>a[j];}

cout<<"case "< sum1=qiuzhi(a,shouwei,weishu,N);//从第一个数开始,循环把每个都查一遍
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< delete []a;
}
while(ww<=T);

cin>>N;

}

回答2:

#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");
}
}
}

回答3:

动态规划