`

NENU 最大连续和

 
阅读更多

模板题,直接使用最大连续子段和即可。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int n;
int a[50005];

int maxsum(int a[], int n)
{
    int start,end;
    int tmp = a[0];
    int max = a[0];
    int i, tmpstart = 0, tmpend = 0;
    start = 1;
    end = 1;
    for(i = 1; i < n; i++)
    {
        if(tmp >= 0) //可以是>0 也可以是>=0,看怎么要求
        {
            tmp += a[i];
            tmpend = i;
        }
        else
        {
            tmp = a[i];
            tmpstart = i;
            tmpend = i;
        }
        if(max < tmp)
        {
            start = tmpstart + 1;
            end = tmpend + 1;
            max = tmp;
        }
    }
    return max;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    int res=maxsum(a,n);
    printf("%d\n",res);
    return 0;
}

 

分享到:
评论

相关推荐

    nenu acm 模板

    nenu acm 模板,虽然不是全部原创,但融合了很多现有模板,并加入了部分自己的东西,全面了模板的注释。

    NENU.Nice.抢答器

    本产品是抢答器的V1版本,包括服务端和客户端。服务端包含SocketServer和后台管理系统。后台管理系统有“用户管理”,“题目管理”,“上传题库”,“举办竞赛”等功能。客户端是一个android手机的apk程序,有抢答,...

    NENU_植物系统与进化

    NENU_植物系统与进化

    android launcher

    android launcher4.0 未改动

    mac扫描器3.1(nbtscan)注册版

    http://opac.nenu.edu.cn/html/ruanjianxiazai/virus/20080317/533.html tuzi提示,机器号即注册码,这个程序可以在arp爆发的时候准确的获得物理地址和ip地址以及机器名等的对应关系,配合arp 防火墙使用,可以迅速...

    联系人信息管理ASP.NET(C#)

    NENU曹毅老师期中作业 ASP.NET(C#)联系人信息管理系统

    FPGA实现VGA的字符显示

    利用FPGA实现VGA显示字符NENU,并且通过按键实现字符的移动,VGA使用ADV7123芯片

    第三周作业 Git和概要设计1

    设计文档请提交到任务2的个人项目仓库中,并可以下载任务2: 创建个人的项目仓库,并把项目仓库的网址URL发到老师邮箱zouxh766@nenu.edu.cn3、

    gatsby-simple-starter:我的盖茨比简单入门工具,用于测试

    gatsby-简单启动器 具备网站所需的基本知识的入门者 从您的CLI运行此启动程序(假设已安装Gatsby): gatsby new gatsby-site https://github.com/nenu-git/gatsby-simple-starter 在开发中运行 yarn start

    demo.html

    demo.html

Global site tag (gtag.js) - Google Analytics