当前位置:网站首页>动态规划问题03_最大子段和
动态规划问题03_最大子段和
2022-07-25 10:35:00 【努力的clz】
- 题目描述:
给定由n个整数(包含负整数)组成的序列:a1、a2、a3……an,求该序列子段和的最大值。- 输入:
若干个整数- 输出:
子序列和的最大值
案例输入输出:
- Input:
1 -3 7 8 -4 12 -10 6- Output:
23
#include<iostream>
using namespace std;
int a[128];
int n;
int MaxSum(){
int sum = 0;
int b = 0;
for(int i=1; i<=n; i++){
if(b>0){
b += a[i];
} else{
b = a[i];
}
if(b>sum){
sum = b;
}
}
return sum;
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i];
}
cout<<MaxSum()<<endl;
return 0;
}
边栏推荐
猜你喜欢

让运动自然发生,FITURE打造全新生活方式

全网显示 IP 归属地,是怎么实现的?

Reptile foundation I

PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying

What kind of product power does Hongguang miniev, the top seller of new energy, have?
Learn NLP with Transformer (Chapter 3)

Nowcodertop12-16 - continuous updating

How can you use unity without several plug-ins? Unity various plug-ins and tutorial recommendations

Activity registration | play with kubernetes container service improvement class officially opened!

Let sports happen naturally, and fire creates a new lifestyle
随机推荐
HCIP(13)
shell-第八章练习
Signal integrity (SI) power integrity (PI) learning notes (XXXIV) 100 rules of thumb for estimating signal integrity effects
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
Nowcodertop1-6 - continuous updating
LVS负载均衡之LVS-NAT与LVS-DR模式原理详解
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
JS convert pseudo array to array
MySQL | GROUP_ The concat function concatenates the values of a column with commas
There is a newline problem when passing shell script parameters \r
NowCoderTOP12-16——持续更新ing
基于MATLAB的常见线性调制方法
Redis 入门
MLX90640 红外热成像仪测温模块开发笔记(五)
让运动自然发生,FITURE打造全新生活方式
Esp32c3 based on the example tutorial of esp32 Rainmaker development under Arduino framework
PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying
世界上最高效的笔记方法(改变你那老版的记笔记方法吧)
Hcip experiment (02)
LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集