自助wap建站,企业营销型网站建设哪家好,忻州市建设厅网站首页,开发公司个人总结分析#xff1a; 如果使用两个for循环遍历所有情况#xff0c;运行会超时解决运行超时的关键点在于#xff1a;及时停止累加#xff0c;丢弃当前的子序列 比如【1#xff0c;-2#xff0c;3#xff0c;10】从第一个数字开始的子序列的和小于从第三个数字开始的子序列的和… 分析 如果使用两个for循环遍历所有情况运行会超时解决运行超时的关键点在于及时停止累加丢弃当前的子序列 比如【1-2310】从第一个数字开始的子序列的和小于从第三个数字开始的子序列的和因为第一到第二的和 sum0sum33所以丢弃这个只会带来负作用的子序列重新开始 package no1_1;
import java.util.*;
import java.io.*;
public class Main {public static void main(String[] args) throws NumberFormatException, IOException {BufferedReader readernew BufferedReader(new InputStreamReader(System.in));int nInteger.parseInt(reader.readLine());String[] sreader.readLine().split( );int[] numbernew int[n];for(int i0;in;i) {number[i]Integer.parseInt(s[i]);}int maxInteger.MIN_VALUE;for(int i0;in-1;i) {int sum0;for(int ji;jn;j) {if(sum0) {//前面累加的和sum小于0时丢弃该组序列因为sum0时sumnumber[i]number[i]break;}else {sumnumber[j];maxMath.max(max, sum);}}}System.out.println(max);}
}