返回一个整数数组中最大子数组的和02

 

 这个要求现在大概算是完成了吧

输入long的范围以内的数都不会有数据溢出

 1 import java.io.BufferedReader;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileReader;
 4 import java.io.IOException;
 5 import java.util.Scanner;
 6 
 7 public class t2 {
 8     public static long max(long a,long b) {
 9         return a>b?a:b;
10     }
11 
12     public static void main(String[] args){
13         long peak=0,drop=0;
14         String s = "";
15         BufferedReader br= null;
16         try {
17             br = new BufferedReader(new FileReader("src\\input.txt"));
18         String line = null;
19         while(true)
20         {
21 
22             if (!((line=br.readLine())!=null)) break;
23             s+=line;
24 //            System.out.println(s);
25         }
26         String[] str = s.split(",");
27         int x = Integer.parseInt(str[0]);
28         int y = Integer.parseInt(str[1]);
29         long[] point = new long[x*y];
30         for(int i=0;i<x*y;i++)
31         {
32             point[i] = Long.parseLong(str[i+2]);
33         }
34         for(int z=0;z<x*y;z++){
35             if(z==0){
36                 peak = point[z];
37                 drop = point[z];
38             }else{
39                 if (drop<0){
40                     drop = point[z];
41                 }else{
42                     drop+=point[z];
43                 }
44             }
45             peak = (peak>drop)?peak:drop;
46         }
47         System.out.println("最大子数组之和为"+peak);
48         } catch (IOException e) {
49             System.out.println("出现异常,程序终止!!!");
50             e.printStackTrace();
51         }
52     }
53 }

 

posted @ 2022-03-19 23:15  SHINIAN200  阅读(30)  评论(0)    收藏  举报