其他分享
首页 > 其他分享> > 数组子数组之和最大(2)

数组子数组之和最大(2)

作者:互联网

 

 

package diyibufen;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class erbufen {
     public static void main(String[] args) throws IOException  {
         int length;
         int[] a=new int[10000];
         BufferedReader br= new BufferedReader(new FileReader("E:\\Java\\diyibufen\\input.txt"));
         String[] s1=br.readLine().split(",");
         
         length=Integer.parseInt(s1[0]); 
         
         String[] s2=br.readLine().split(",");
         for(int i=0;i<length;i++) 
         {
             a[i]=Integer.parseInt(s2[i]);
         }
         
         int maxsum=0,sum=0;
         maxsum=a[0];
         for(int i=0;i<=length;i++)
         {
             sum = sum + a[i];
             if(sum<0) 
                 sum=0;
             
             if(sum>maxsum)
                 maxsum=sum;
         }
         
         if(maxsum==0) {
             maxsum=a[0];
             for(int i=1;i<length;i++) {
                 if(a[i]>maxsum)
                     maxsum=a[i];
             }
         }
         
         System.out.println("最大子数组的和为:" + maxsum);
         
     }

            
    }

 

标签:java,最大,io,int,br,数组,import,maxsum
来源: https://www.cnblogs.com/15132949hao/p/16028464.html