题解 | #求连续子数组的最大和# 前缀和

求连续子数组的最大和

https://www.nowcoder.com/practice/8705437354604a7cb9ba7bf959e42de6

#include <iostream>
#include <vector>

using namespace std;
vector<int> a;

int main() {
    int x = 0;
    char c;
    int p = 1;
    while((c=getchar()) != '\n'){
        if(c == '-'){
            p = -1;
        }else if(c == ','){
            a.push_back(x*p);
            p = 1;
            x = 0;
        }else{
            x = x*10+(c-'0');
        }
    }
    a.push_back(x*p);
    a.insert(a.begin(), 0);

    int n = a.size();
    vector<int> pre(n+1,0);

    for(int i = 1; i <= n; i++){
        pre[i] = pre[i-1]+a[i];

    }
    int res = 0;
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            res = max(res ,pre[j]-pre[i-1] > 0 ? pre[j]-pre[i-1] : 0);
        }
    }
    cout << res << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

#每日一题#
全部评论

相关推荐

只有一个苍穹外卖外加正在看黑马点评,可以找小厂实习吗,还有我的简历有什么大问题吗
Java抽象小篮子:感觉有点熟悉,问题1是学历,2是没实习经历,3是专业技能写得太少太少了(怎么写可以看我置顶帖),4是仅这一个项目找实习不够看。拷打完毕,简历怎么写可以看我置顶帖子
点赞 评论 收藏
分享
豆泥🍀:同26届,加油,我也还没找到查看图片
点赞 评论 收藏
分享
05-30 18:54
武汉商学院 Java
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务