在一个整数数组中求子串的最大和

package FirstDay;
/**
 * 输出一个整数序列最大子串和
 */
public class BiggestSum {
    public  static void getSum(int n,int mat[]) {
        int max=0,max2=0;
        while(n>0) {
            for(int j=0;j<n;j++) {
                int sum=0;
                for(int m=j;m<n;m++) {
                    sum=sum+mat[m];
                }
                if(sum>max) {
                    max=sum;
                }
            }
            if(max>max2) {
                max2=max;
            }
            n--;
        
        
        }
        System.out.println(max2);
    }
    public static void main(String[] args) {
        int mat[]= {-1,2,3,-9,-8};
        getSum(5, mat);
        System.out.println("计算完毕");
    }

}

猜你喜欢

转载自blog.csdn.net/sd116460/article/details/81349829