package shuzu; import java.util.*; import java.util.Scanner; public class aaa { public static void main(String[] args) throws InterruptedException { Scanner sc = new Scanner(System.in); int n=0; int linshin=0; Scanner input=new Scanner(System.in); int nshu; System.out.print("请输入数组中数的个数:"); int num=input.nextInt(); int shu[]=new int[num]; for(int i=0;i<num;i++)//循环生成num个随机数 { if((int)(Math.random()*2)==0)//先从0或1之间生成一个数,进而决定这个数的正负 { shu[i]=(int)(Math.random()*10); } else { shu[i]=-(int)(Math.random()*10); } } for(int i=0;i<num;i++)//循环输出生成的num个随机数 { System.out.println(shu[i]); } int max = shu[0];//存储最大的和 int linshimax=1; System.out.println("0顺序输出,其他数字为指定输出,请输入"); for(nshu=0;nshu<5;nshu++) { int n1 = 0; int n2 = 0; for(int nnshu=nshu;nnshu<5;nnshu++) { int type = sc.nextInt(); if(type==0) { n1 = n1 + shu[nnshu]; int zongmax=0; if(nnshu<4) { zongmax=max; nnshu = nnshu + 1; n2 = n1 + shu[nnshu]; max = maxxx(n1,n2,max); nnshu = nnshu - 1; }else { zongmax=max; max = maxx(n1,max); } linshin++; System.out.println("当前计算到第" + linshin + "个子数组"); if(zongmax!=max) { System.out.println("最大子数组为第" + linshin + "个子数组"); linshimax=linshin; }else { System.out.println("最大子数组为第" + linshimax + "个子数组"); } System.out.println("当前运行到第" + (nshu+1) + "个到第" + (nnshu+1) + "个"); System.out.println("最大值为:" + max); Thread.sleep(1000); }else { linshin=0; n=type; for(nnshu=nshu;nnshu<5;nnshu++) { n1 = n1 + shu[nnshu]; int zongmax=0; if(nnshu<4) { zongmax=max; nnshu = nnshu + 1; n2 = n1 + shu[nnshu]; max = maxxx(n1,n2,max); nnshu = nnshu - 1; }else { zongmax=max; max = maxx(n1,max); } linshin++; if(linshin==n) { System.out.println("当前计算到第" + linshin + "个子数组"); if(zongmax==max) { System.out.println("最大子数组为第" + linshimax + "个子数组"); }else { System.out.println("最大子数组为第" + linshin + "个子数组"); } System.out.println("当前运行到第" + (nshu+1) + "个到第" + (nnshu+1) + "个"); System.out.println("最大值为:" + max); Thread.sleep(1000); break; } } } } } } static int maxxx(int a,int b,int ab) { int max; if(a<b) { max = b; if (max<ab) { max = ab; } }else { max = a; if(max<ab) { max = ab; } } return max; } static int maxx(int a , int b){ int max; if(a<b) { max = b; }else { max = a; } return max; } }