算法题——最大子序列之和

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

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);
    }
}


猜你喜欢

转载自blog.csdn.net/cm9393/article/details/77603510