题目:给定一个数组数据,在数组可以循环的情况下求连续的n个数据的最小值,n为数组下标。
源代码:
1 package first; 2 3 import java.util.Scanner; 4 5 public class xunhuan { 6 7 public static void main(String[] args) { 8 9 int i,j; 10 int sum=0; 11 int count=1; 12 int max; 13 Scanner sc=new Scanner(System.in); 14 System.out.print("输入数组的长度: "); 15 i=sc.nextInt(); 16 int length=i*i; 17 int []x=new int[i]; 18 int []y=new int[2*i-1]; 19 int []z=new int[length]; 20 System.out.println("请输入数组元素"); 21 for(i=0;i<x.length;i++) 22 { 23 x[i]=sc.nextInt(); 24 } 25 for(i=0;i<y.length ;i++) 26 { 27 if(i<x.length) 28 { 29 y[i]=x[i]; 30 } 31 else 32 y[i]=x[i-x.length]; 33 } 34 System.out.println(" "); 35 36 37 for(i=0;i<x.length;i++) 38 { 39 sum=0; 40 for(j=i;j<i+x.length;j++) 41 { 42 sum=sum+y[j]; 43 44 z[count-1]=sum; 45 count++; 46 } 47 } 48 max=z[0]; 49 for(i=0;i<z.length;i++) 50 { 51 if(max<z[i]) 52 max=z[i]; 53 } 54 55 System.out.println("最大子数组的和为:"+max); 56 } 57 58 }