Max sum 求相邻数最大和

Problem Description

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence.; For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.;

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases.; Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000; and 1000).

Output

For each test case, you should output two lines.; The first line is "Case #:", # means the number of the test case.; The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence;, the end position of the sub-sequence.; If there are more than one result, output the first one.; Output a blank line between two cases.;

Sample Input

2

5 6 -1; 5 4 -7;

7 0 6 -1; 1 -6; 7 -5;

Sample Output

Case 1:

14 1 4

Case 2:

7 1 6

   C语言,使用结构体
#include<stdio.h>
struct T 
{
 int st,en,nu;
}max[100005];

int main()
{
 int t,n,temp,k,i,m;
 scanf("%d",&t);
 k=0;
 while (t--)
 {
  k++;
  scanf("%d",&n);
  for (i=1;i<=n;i++)
  {
   scanf("%d",&max[i].nu);
   max[i].st=max[i].en=i;
  }
  temp=max[1].nu;m=1;
  for (i=2;i<=n;i++)
  {
   if (max[i-1].nu>=0)
   {
    max[i].nu+=max[i-1].nu;
    max[i].st=max[i-1].st;
   }
   if (max[i].nu>temp)
   {
    temp=max[i].nu;
    m=i;
   }
  }
  printf("Case %d:\n%d %d %d\n",k,temp,max[m].st,max[m].en);
  if (t!=0)
  {
   printf("\n");
  }

 }
 return 0;
}

                                C语言使用数组实现
#include<stdio.h>   
int main()
{
 int t,n,i,j,k,ia[100005],ib[100005],k1,k2,d,s;
 scanf("%d",&t);
 d=1;
 while(t--)
 {
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
   scanf("%d",&ia[i]);
   ib[i]=i;
  }

  k=ia[1];k1=1;k2=1;
  for(i=2;i<=n;i++)
  {
   if(ia[i-1]>=0)
   {
    ia[i]+=ia[i-1];
    ib[i]=ib[i-1];
   }
   if(ia[i]>k)
   {
    k=ia[i];
    k2=i;
   }
  }
  printf("Case %d:\n%d %d %d\n",d,k,ib[k2],k2);
  d++;
  if(t!=0)
   printf("\n");
 }
 return 0;
}

                                C语言代码,来自:http://blog.csdn.net/iori31/
#include<stdio.h>  
int b,e,i,k,t,n,a,s[100001],m;  
int main(){  
    for(scanf("%d",&t),k=1;k<=t;++k){  
        for(scanf("%d%d",&n,&a),m=s[0]=a,e=0,i=1;i<n;++i){  
            scanf("%d",&a);  
            if(s[i-1]>=0&&s[i-1]+a>=0)s[i]=s[i-1]+a;  
            else s[i]=a;//s[i-1]<0时 无论a(可看作a[i])值多少 s[i]=a都合理 而且s再加上之前的和不可能成为最大值  
            if(s[i]>m)m=s[i],e=i;//若出现多个最大值 end始终在第一个  
        }  
        for(i=e;i>=0;--i)  
            if(s[i]<0)break;//若找不到s<0,任何情况都b=0  
        b=m>=0?i+1:i,e++,b++;//being和end自增后可直接输出 m<0必有b=e  
        printf("Case %d:\n%d %d %d\n%s",k,m,b,e,k<t?"\n":"");  
    }  
}  

                                C++代码,Crazy_Frog提供
#include <iostream>  
using namespace std;  
void main()  
{  
    int n,c,num,begpos,endpos,p,sum,maxsum;  
    int i, j;  
    cin>>n;  
    for(i=1;i<=n;i++)  
    {  
        begpos=1;  
        endpos=1;  
        p=1;  
        sum=0;  
        maxsum=-1000;  
        cin>>c;  
        for(j=1;j<=c;j++)  
        {  
            cin>>num;  
            sum+=num;  
            if(sum>maxsum)  
            {  
                maxsum=sum;  
                begpos=p;  
                endpos=j;  
            }  
            if(sum<0)  
            {  
                p=j+1;  
                sum=0;  
            }  
        }  
        cout<<"Case "<<i<<":"<<endl;  
        cout<<maxsum<<" "<<begpos<<" "<<endpos;  
        if(i==n) cout<<endl;  
        else cout<<endl<<endl;  
    }  
}  

                                C++代码2
#include <iostream>
using namespace std;

int max_sum(int* data, int n, int& s, int& e)
{
    int ans = -2147483640, curr = 0;
    int start = 0, end = 0;
    for (int i = 0, j = 0; i < n; ++i)
    {
        curr += data[i];
        if (curr > ans) ans = curr, s = j, e = i;
        if (curr < 0) curr = 0, j = i + 1;
    }
    return ans;
}

int data[100005];
int main()
{
    int cas;scanf("%d", &cas);
    for (int curr = 1;  curr <= cas; ++curr)
    {
        if (curr > 1) puts("");
        int n, s, e;scanf("%d", &n);
        for (int i = 0; i < n; ++i)scanf("%d", data+i);
        printf("Case %d:\n%d", curr, max_sum(data, n, s, e));
        printf(" %d %d\n", s+1, e+1);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值