连续子序列最大和

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/flower_CSDN/article/details/82751076

去哪儿笔试题一

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

}

猜你喜欢

转载自blog.csdn.net/flower_CSDN/article/details/82751076