package maxnumber; import java.util.*; /* * 寻找各个数开头的最大值,进行比较 * * */ public class two { /*public static int sum() { return max1; }*/ public static void main(String[] args) { Scanner input=new Scanner(System.in); int n=0,number=1; System.out.print("请输入数的个数:"); n=input.nextInt(); System.out.print("请输入"+n+"个数:"); int a[]=new int[5]; for(int i=0;i<5;i++) { //输入 a[i]=input.nextInt(); } int max[]=new int[a.length]; for(int i=0;i<max.length;i++) { max[i]=0; } int sum=0; for(int i=0;i<a.length;i++) { //以各个数开头的最大值 //System.out.println("以第"+(i+1)+"个数开头的"+(n-i)+"个数"); try{Thread.sleep(100);}catch(Exception e){} max[i]=a[i]; sum=max[i]; System.out.println("第"+number+"步:以第"+(i+1)+"个数开头的1个数的最大值:"+max[i]+" 已检查"+(number-i)+"个子数组"); number++; try{Thread.sleep(100);}catch(Exception e){} if(i<a.length) { for(int j=i+1;j<a.length;j++) { sum=sum+a[j]; if(max[i]<sum)max[i]=sum; System.out.println("第"+number+"步:以第"+(i+1)+"个数开头的"+((j+1)-(i+1)+1)+"个数的最大值:"+max[i]+" 已检查"+(number-i)+"个子数组"); number++; try{Thread.sleep(100);}catch(Exception e){} } System.out.println("第"+number+"步:以第"+(i+1)+"个数开头的最大值:"+max[i]); number++; try{Thread.sleep(100);}catch(Exception e){} } } int max1=max[0]; int k=1; for(int i=1;i<max.length;i++) { if(max1<=max[i]) { max1=max[i]; System.out.println("第"+number+"步:第"+(i+1)+"个数开头的最大值大于第"+k+"个数开头的最大值"); k=i+1; number++; try{Thread.sleep(100);}catch(Exception e){} } else { System.out.println("第"+number+"步:第"+k+"个数开头的最大值大于第"+(i+1)+"个数开头的最大值"); number++; try{Thread.sleep(100);}catch(Exception e){} } } System.out.println("综上,最大值为:"+max1); } }