最大子序列之和

来源:互联网 发布:淘宝店主风怎么调色 编辑:程序博客网 时间:2024/05/16 05:13

求数组中最大连续子序列之和。

eg,输入一行数据 1 3 -2 4 -5

  输出最大连续子序列和 6

import java.util.*;public class test{    public static void main(String[] args){        Scanner in = new Scanner(System.in);        ArrayList<Integer> list = new ArrayList<Integer> ();         while(in.hasNext()){        list.add(in.nextInt());        }        int len = list.size();        int max ,maxhere;        max = maxhere = list.get(0);        for(int i = 1; i < len; i++){        if(maxhere <= 0)        maxhere = list.get(i);        else        maxhere += list.get(i);        if(maxhere > max){        max = maxhere;        }        }        in.close();                System.out.println(max);
    }}


原创粉丝点击