一维数组debug

package test;

import java.util.*;

public class test {
  
    
     public static void main(String[] args) throws InterruptedException {
    	 Scanner sc=new Scanner(System.in);
         System.out.print("请输入数组中数的个数:");
         int n=sc.nextInt();
         
         System.out.print("请输入"+n+"个整数:");
         int a[]=new int[n]; 
         for(int i=0;i<n;i++)//n个随机数
         {   
           a[i]=sc.nextInt();
     }
         
  
         int b =  FindGreatestSumOfSuba(a);
   
         System.out.println("连续子数组的最大和为:"+b);
         Thread.sleep(1000);
     }
  
     public static int FindGreatestSumOfSuba(int[] a) throws InterruptedException {
            int len = a.length;
            if (len == 0){
             return 0;
            }
            int[] c = new int[len];
            c[0] = a[0];
            int d = a[0];
            System.out.println("第1步:累加子数组和:"+c[0]+",最大子数组和:"+d);
            Thread.sleep(1000);
            for(int i=1;i<a.length;i++){
                 if(c[i-1]>0){
                     c[i] = c[i-1] + a[i];
                 }else{
                     c[i] = a[i];
                 }
                 if(c[i] > d){
                     d  = c[i];
                 }
                System.out.println("第"+(i+1)+"步:累加子数组和:"+c[i]+",最大子数组和:"+d);
                Thread.sleep(1000);
            }
            return d;
     }
}

  

 

posted @ 2019-04-25 19:27  张利杰j  阅读(143)  评论(0编辑  收藏  举报