Live2D

数组

public static int maxSubarraySumCircular(int[] A) {

        if(A==null||A.length<1){

            return 0;

        }

        int curMax,max,curMin,min,sum;

        curMax = max = curMin =min = sum = A[0];

        for(int i=1;i<A.length;i++){

            sum+=A[i];

            curMax = Math.max(A[i],curMax+A[i]);

            curMin = Math.min(A[i],curMin+A[i]);

            max = Math.max(max,curMax);

            min = Math.min(min,curMin);

        }

        if(max<0)

            return max;

        return Math.max(sum-min,max);

}

public static void main(String[] args) {

int i = 0, n;

int a[] = new int[100];

Scanner scanner = new Scanner(System.in);

n = scanner.nextInt();

for (i = 0; i < n; i++) {

a[i] = scanner.nextInt();

}

int max = maxSubarraySumCircular(a);

scanner.close();

System.out.println(max);

}

 

public static void main(String[] args) throws IOException {

BufferedReader bufferedReader = new BufferedReader(newFileReader(new File("F:\\WorkSpace\\Test\\src\\Teste\\1.txt")));

List<BigInteger> a = new  ArrayList<>();

String file;

BigInteger max_ending_here,max_so_for;

max_ending_here = max_so_for = BigInteger.valueOf(Integer.MIN_VALUE);

while((file=bufferedReader.readLine())!=null) {

try {

BigInteger x = BigInteger.valueOf(Long.parseLong(file));

System.out.println(x);

a.add(x);

} catch (Exception e) {

System.err.println("存在非法字符 程序将退出!");

return;

}

}

bufferedReader.close();

for(BigInteger x:a) {

max_ending_here = x.max(max_ending_here.add(x));

max_so_for =max_so_for.max(max_ending_here);

}

System.out.println("-----------");

System.out.println(max_so_for);

 

}

posted @ 2020-02-26 18:01  NyerROii  阅读(54)  评论(0)    收藏  举报