5.J - Max Sum

题目连接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int t,n,now,before,max,s,e,x,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&now);
            if(i==1)//注意是等号等号!!!不要写错
            {
                max=before=now;
                s=x=e=1;
            }
            else
            {
                if(now>now+before)//如果前面的是负数就从当前开始
                {
                    before=now;
                    x=i;
                }
                else before+=now;
                if(before>max)//加了当前的值后,大于max,才更新max,保证了max 是最大值
                {
                    max=before;
                    s=x;
                    e=i;
                }
            }

        }
        printf("Case %d:\n%d %d %d\n",++cnt,max,s,e);
            if(t) printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Twsc/p/5754067.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值