JAVA实现代码,注意要使用InputStreamReader 来处理较大的数据量。【代码有解释】
import java.util.Scanner;
import java.io.*;
public class Main {
private static Reader reader;
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
reader=new InputStreamReader(System.in);
int n=get_int();
int minSum=0,maxSum=0;
for (int a=0;a<n;a++){
minSum+=get_int();
if (minSum>maxSum){
maxSum=minSum;
}
if (minSum<0)
minSum=0;
}
System.out.println(maxSum);
}
//inputStreamReader 解决文本大量数据的输入
public static int get_int(){//完成一次数字输入
int res=0,flag=1,read=0;
try {
while((read=reader.read())!=-1){
if (read == '-'){//符号
flag=-1;
}else if (Character.isDigit(read)){//是数字
res=read-'0';
while ((read=reader.read())!=-1){//下一个存不存在,
if (Character.isDigit(read))//存在,判断是数字
res=res*10+(read-'0');
else//不是数字,跳出第一个while 完成一个数字的输入
break;
}
break;//再跳,因为已经完成了一个数字的输入,没必要再判断
}
}
} catch (IOException e) {
e.printStackTrace();
}
return res*flag;
}
}