连续子序列最大和

去哪儿笔试题一

package sansix;

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String str=sc.nextLine();
		String[] s=str.split(" +");
		int[] num=new int[s.length];
		int[] temp=new int[s.length];
		for (int i = 0; i < s.length; i++) {
			num[i]=Integer.parseInt(s[i]);
			temp[i]=Integer.parseInt(s[i]);
		}
		Arrays.sort(temp);
		int max=0;
		if(temp.length>0) {
			max=temp[temp.length-1];
		}
		
		int res=0;
		for (int i = 0; i < num.length; i++) {
			for (int j = i; j < num.length; j++) {
				res+=num[j];
				if(res>max) {
					max=res;
				}
			}
			res=0;
		}
		
		System.out.println(max);
	}

}

posted @ 2018-09-18 00:12  快乐的内啡肽呀  阅读(16)  评论(0)    收藏  举报