题目描述
给定整数序列A。 求在整数序列A中连续权值最大的子序列的权值。
输入描述:
第一行输入整数n.(1<=n<=1000) 第二行输入n整数a。(-1000<=a<=1000)
输出描述:
输出子序列最大权值。
代码实现:
import java.util.ArrayList;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str_0 = scan.nextLine().trim();
int n = Integer.parseInt(str_0);
String str_1 = scan.nextLine();
String[] line_list_1 = str_1.trim().split(" ");
ArrayList<Integer> arr = new ArrayList<>();
for(int i = 0; i < line_list_1.length; i++){
arr.add(Integer.parseInt(line_list_1[i]));
}
scan.close();
int result = solution(n, arr);
System.out.println(result);
}
public static int solution(int n, ArrayList<Integer> arr){
int result = 0;
for(int i=0;i<n;i++){
if(arr.get(i)<=0){
continue;
}
int tmp=0;
for(int j=i;j<n;j++){
tmp+=arr.get(j);
result=Math.max(tmp,result);
}
}
return result;
}
}
长时间观察CSDN每日一练,发现在很多情况,简单题比困难题更难。也不知道是官方故意这么做的,从而来激发博客们敢于做难题的信心,还是别的。。。,不管官方怎么去思考,我还是希望官方能够重新认真调整题目的难易标签,(更方便)使博客们根据自身情况能够找到适合自己的题目。