课上作业 最大子

package shuzu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
public class Test {
    public static void main(String[] args) throws IOException {
        try{
            FileWriter fw =new FileWriter("a.txt");
        //随机数
        for(int i=0;i<10000000;i++) {
            long x=(long)(Math.random()*1000000000)*(Math.random()>0.5?1:-1);
            String y=""+x;
            fw.write(y);
            fw.write("\r\n");
            System.out.println(y);
        }
        fw.close();}
        catch(FileNotFoundException e){
            e.printStackTrace();
        }

        ArrayList<String> arrList = new ArrayList<>();
        try {
            FileReader fr = new FileReader("a.txt");
            BufferedReader bf = new BufferedReader(fr);
            String st;
            while ((st = bf.readLine()) != null) {
                arrList.add(st);
            }
            bf.close();
            fr.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    long a[] = new long[arrList.size()];
    for (int i = 0; i <arrList.size(); i++) {
        //数组转移
        String s=arrList.get(i);
        a[i] = (long) Double.parseDouble(s);
        }
    
    long max=a[0];
    long sum=a[0];
    for(int i=1;i<a.length;i++) {
        if(sum<0)
            sum=a[i];
        else 
            sum+=a[i];
        if(sum>max)    max=sum;
    }
    System.out.println(max);
    }
}
anCode

 

 

猜你喜欢

转载自www.cnblogs.com/yang-qiu/p/10548506.html
0条评论
添加一条新回复