当前位置:网站首页>7-3 最大子段和
7-3 最大子段和
2022-06-24 19:44:00 【白—】
7-3 最大子段和
给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。当所给的整数均为负数时,定义子段和为0。
要求算法的时间复杂度为O(n)。
输入格式:
输入有两行:
第一行是n值(1<=n<=10000);
第二行是n个整数。
输出格式:
输出最大子段和。
输入样例:
在这里给出一组输入。例如:
6
-2 11 -4 13 -5 -2
输出样例:
在这里给出相应的输出。例如:
20
代码:
#include <stdio.h>
#include <stdlib.h>
int n;
int a[10100];
int vis[10100];
int findmax(int a,int b)
{
return a>=b?a:b;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int max1=0;
int temp;
for(int i=1;i<=n;i++)
{
if(temp>max1)
max1=temp;
if(temp<0)
temp=a[i];
else
temp+=a[i];
}
printf("%d",max1);
return 0;
}
202206222059三
边栏推荐
猜你喜欢
![[JS] - [string - application] - learning notes](/img/dc/f35979b094f04c0ee13b3354c7741d.png)
[JS] - [string - application] - learning notes

【js】-【数组、栈、队列、链表基础】-笔记

案例解析:用「度量」提升企业研发效能|ONES Talk

Installation and deployment of ganglia

Laravel pagoda security configuration

Online group chat and dating platform test point

伪原创智能改写api百度-收录良好

Blogs personal blog test point (manual test)

File contains vulnerability issues

记录一下MySql update会锁定哪些范围的数据
随机推荐
[JS] - [linked list - application] - learning notes
idea创建模块提示已存在
Chapter VI skills related to e-learning 5 (super parameter verification)
[basic knowledge] ~ half adder & full adder
【js】-【數組、棧、隊列、鏈錶基礎】-筆記
Selection (029) - what is the output of the following code?
斐波那契
378. Knight placement
379. hide and seek
Installation and deployment of ganglia
Financial management [5]
【js】-【数组、栈、队列、链表基础】-笔记
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息、解读模型系数交互作用及其显著性
Basic data type
laravel 创建 service层
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)
文件包含漏洞问题
R语言dplyr包group_by函数和summarise_at函数计算dataframe计算不同分组的计数个数和均值(Summarise Data by Categorical Variable)
【基础知识】~ 半加器 & 全加器
Still using simpledateformat for time formatting? Be careful of project collapse