zoukankan      html  css  js  c++  java
  • 最大字段和 51nod 1049 水水水水水水水水水水水水

    N个整数组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连续子段和的最大值。当所给的整数均为负数时和为0。
     
    例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13。和为20。
    Input
    第1行:整数序列的长度N(2 <= N <= 50000)
    第2 - N + 1行:N个整数(-10^9 <= A[i] <= 10^9)
    Output
    输出最大子段和。
    Input示例
    6
    -2
    11
    -4
    13
    -5
    -2
    Output示例
    20

    水题 ,不懂问我,只限女生。
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 #include<stack>
    16 #include<vector>
    17 int a[51000];
    18 int main()
    19 {
    20     int n;
    21     cin>>n;
    22     for(int i=0;i<n;i++)
    23         scanf("%d",&a[i]);
    24     long long  sum=0;
    25     long long  max1=0;
    26     for(int i=0;i<n;i++)
    27     {
    28         sum+=a[i];
    29         if(sum<0)
    30             sum=0;
    31         max1=max(sum,max1);
    32     }
    33     cout<<max1<<endl;
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    Python从入门到精通系列文章总目录
    使用465端口加密发邮件
    kubernetes学习14—Dashboard搭建和认证
    kubernetes学习01—kubernetes介绍
    CSS基础
    SVN 命令行的使用
    Python判断字符集
    Flask框架(2)-JinJa2模板
    搭建ntp服务器
    Ansible的Playbook的编写
  • 原文地址:https://www.cnblogs.com/dulute/p/7499105.html
Copyright © 2011-2022 走看看