定义一个数组返回最大子数组的值(1)

对于老师上课的题目,要想获得子函数最大值,首先进行数组的遍历,在遍历起初,对最大值进行初始化为数组的第一个元素,每次遍历,求得该子数组的和,并将此和与最大值进行比较,若小于

最大值,则进行下一次的遍历,直到结束。此处用到三个for循环,来进行次数的控制。第一个 for(i = 0; i <length; i++),用来完成所有数组的循环,第二个for(j = i; j

<length; j++)用来表示从第几个元素开始,寻找子数组,第三个 for( k = i; k <= j; k++) ,用来获取每个子数组的和,等到遍历完全结束,返回最大值输出。

 

 

下面是具体的代码:

package 数组;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;
import javax.sound.sampled.Line;
public class main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  final  int N=500000;//可读取的数据量N
  main c=new main();
int[] A=new int[N];
String[] B=new String[1];
String C=new String();
String[] D=new String[10];
Scanner in=new Scanner(System.in);
A=c.randomCommon(0,294967295,N);
c.write(A,N);
 int q;
 
 B=c.read();
 C=B[0];
 D=C.split(" ");
 for(int n=0;n<N;n++)
 {
  try {
   A[n]= Integer.parseInt(D[n]);
  } catch (NumberFormatException e) {
   System.out.println("输入的第"+(n+1)+"个不是整数(或范围过大):"+D[n]);
     // e.printStackTrace();
      System.exit(0);
  }
  
 }
 System.out.println(A[6]);
 
 q=c.max(A,10);
 System.out.println(q);
 }
/**
 * @param A
 * @param n
 * @return
 */
public int max(int[] A,int n)
{
 int end=A[0];
 int sum=0;
 for(int i=0;i<10;i++)
 {sum=A[i];
  //end=A[i];//存储子数组中的最大值
  for(int j=i;j<10;j++)
  {
   sum+=A[j];
   if(sum>end)
    end=sum;
   
   
  }
  
 }
 return end;
}
public String[] read()//读出文件
{
  String[] w=new String[10];
 Scanner sc = null;
 try {
  sc = new Scanner(new FileReader("D:\\shuzu.txt"));
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 String line=null;
 while((sc.hasNextLine()&&(line=sc.nextLine())!=null))
 { }
 w[0]=line;
 return w;
}
/**
 * 随机指定范围内N个不重复的数
 * 最简单最基本的方法
 * @param min 指定范围最小值
 * @param max 指定范围最大值
 * @param n 随机数个数
 */   
public int[] randomCommon(int min, int max, int n){ 
    if (n > (max - min + 1) || max < min) { 
           return null; 
       } 
    int[] result = new int[n]; 
    int count = 0; 
    while(count < n) { 
        int num = (int) (Math.random() * (max - min)) + min; 
        boolean flag = true; 
        for (int j = 0; j < n; j++) { 
            if(num == result[j]){ 
                flag = false; 
                break; 
            } 
        } 
        if(flag){ 
            result[count] = num; 
            count++; 
        } 
    } 
    return result; 

public void write(int[] A,int a)//写入文件
{
 File file = new File("D:\\shuzu.txt");
    try {
         FileWriter writer = new FileWriter(file, true);
         for(int i=0;i<a;i++)
         {writer.write(A[i]+" ");}
       
         writer.write("\r\n");
         writer.close();
    } catch (Exception ex) {
         ex.printStackTrace();
         ex.getMessage();
    }
}
}

 

posted @ 2019-03-10 17:40  ZQL2017  阅读(228)  评论(0编辑  收藏  举报