首页 > 编程语言 > 详细

连续子数组最大和

时间:2020-06-21 17:31:49      阅读:72      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

技术分享图片

 

 

import java.util.Scanner;
import java.lang.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i< n; i++){
            arr[i] = in.nextInt(); 
        }
        int res = arr[0];
        int max = arr[0]; //存放连续数组的和
        for(int i = 1; i < n; i++){
            max = Math.max(max + arr[i], arr[i]);//如果连续数组的和小于当前数组i元素的值,那么就从arr[i]开始重新连续
            res = Math.max(res, max);
        }
        System.out.println(res);
    }
}

 

连续子数组最大和

原文:https://www.cnblogs.com/gy7777777/p/13173046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!