01-复杂度1 最大子列和问题 (20分)【java实现】

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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值