首页 > 编程语言 > 详细

求数组连续最大子串和

时间:2017-08-26 16:32:12      阅读:334      评论:0      收藏:0      [点我收藏+]
import java.util.*;
public class Main
    {
    public static void main(String[] args)
        {
        Scanner sr=new Scanner(System.in 

);
        while(sr.hasNext())
            {
            String str=sr.nextLine();
            String[] strs=str.split(" ");
            int[] num=new int[strs.length];
            for(int i=0;i<strs.length;i++)
              {
                num[i]=Integer.valueOf(strs[i]);
            }  
            int sum=0;
            int max=Integer.MIN_VALUE;
            for(int i=0;i<num.length;i++)
                {
                sum+=num[i];
                if(sum>max)
                    max=sum;
                if(sum<0)
                    sum=0;
            }
            System.out.println(max);
        }
    }
}

 

求数组连续最大子串和

原文:http://www.cnblogs.com/mingyao123/p/7435626.html

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