题目(1231)
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int K,num[10010],cnt;
int end,start,thisMax,Max,temp;
while(cin>>K&&K)
{
cnt=0;
for( int i=0; i<K; i++)
{
scanf("%d",&num[i]);
if(num[i] < 0 )
cnt++;
}
if(cnt == K)
{
printf("%d %d %d
",0,num[0],num[K-1]);
continue;
}
start = end = 0;
Max = thisMax = num[0];
for( int i=1; i<K; i++ )
{
if(thisMax < 0)
{
temp = i;
thisMax = 0;
}
thisMax += num[i];
if(thisMax > Max)
{
Max = thisMax;
end = i;
start = temp;
}
}
printf("%d %d %d
",Max,num[start],num[end]);
}
return 0;
}
虽然看了别人的博客AC了但是自己还是不知其所以然。。。题目(1003)
这道题和上面那道题没有多大的区别,可以说是一模一样,唯一有点疑惑的是,上面那道题的temp 没有赋予初值却对,下面这道题就不可以了
#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int T,cnt=0,n;
int num[100010];
int thisMax,Max,temp;
scanf("%d",&T);
while(T--)
{
int start, end;
memset(num,0,sizeof(num));
cnt++;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&num[i]);
start = end = temp = 0;
thisMax = Max = num[0];
for( int i=1; i<n; i++ )
{
if(thisMax < 0)
{
temp = i;
thisMax = 0;
}
thisMax += num[i];
if(thisMax > Max)
{
Max = thisMax;
end = i;
start = temp;
}
}
if(cnt != 1) printf("
");
printf("Case %d:
",cnt);
printf("%d %d %d
",Max,start+1,end+1);
}
return 0;
}