当前位置:网站首页>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三
边栏推荐
猜你喜欢
选择类排序法
Tech talk activity review kubernetes skills of cloud native Devops
How should we measure agile R & D projects?
Jetpack Compose 最新进展
[JS] - [array, stack, queue, linked list basics] - Notes
并发之共享模型管程
Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
【js】-【字符串-应用】- 学习笔记
[JS] - [stack, team - application] - learning notes
03_ Spingboot core profile
随机推荐
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the AIC function to compare the AIC values of the two models (simpl
F29oc analysis
Gocolly manual
golang convert map to json string
Accounting standards for business enterprises application [5]
Pseudo original intelligent rewriting API Baidu - good collection
Case analysis: using "measurement" to improve enterprise R & D efficiency | ones talk
文件包含漏洞问题
【js】-【树】-学习笔记
[JS] - [array, stack, queue, linked list basics] - Notes
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、通过双样本t检验分析(双独立样本t检验)来判断倾向性评分匹配后样本中的所有协变量的平衡情况
Building Survey [1]
Selection (026) - what is the output of the following code?
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数、confint函数、coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比的置信区间
Installation and deployment of ganglia
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
【js】-【栈、队-应用】-学习笔记
Financial management [4]
Latest development of jetpack compose
Learn about redlock