zoukankan      html  css  js  c++  java
  • 返回一个整数数组中最大子数组的和

    2019.3.7

    进行了这个课堂的测试,最大子数组是连续的一个或者多个数组,因为老师要求的复杂度是O(n)

    ,我采用的办法是:用max表示几个连续最大的值,max2表示目前位置的的最大值,最后返回max;

    第二段代码是对第一个的补充,用到了文件流(读取和写入文件),但是还没有实现大数运算。

    第三段代码是实现了biginteger,但是我的电脑竟然计算不出来结果,没有报错啊·············还有随机数是(-1000,1000),之前的没有写负数。

    第四段代码实现了数组首尾相连,求最大子数组。

    package zishuzu;
    import java.util.Arrays;
    import java.util.Scanner;
    public class Sum {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.print("输入数组个数:");
                int n = sc.nextInt();
                System.out.print("输入数组:");
                int a[] = new int[n*2];          
                for(int i=0; i<n; i++)               
                    {
                    a[i] = sc.nextInt();
                    }
                
                for(int j=0;j<n-1;j++)
                {
                    a[n+j]=a[j];
                }
                sc.close();
                String intArrayString = Arrays.toString(a);
                System.out.println(large(a,n));  //打印最大子数组
        }
         private  static int max(int a,int b) {
             if(a>b) return a;
             else return b;
         }
        private  static int large(int a[],int n) {
            int max=a[0];
            int max2=0;       
            for (int i= 0; i<n*2; i++)
            {
            max2=max(max2+a[i],a[0]);   //几个连续最大的值
            max=max(max,max2);     
                 }
            return max;
        }
    }
    package zishuzu;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.math.BigInteger;
    import java.util.ArrayList;
    
    public class Sum {
        public static boolean isInt(String val) {
            try {
            Integer.parseInt(val);
            return true;
            } catch (NumberFormatException e) {
            return false;
            }
            }
        public static void main(String[] args) throws IOException {
            try {
    
                FileWriter fw = new FileWriter("Test.txt");
                BufferedWriter bw = new BufferedWriter(fw);
                for (int i = 0; i < 100; i++) {
                    java.util.Random rnd = new java.util.Random();
                    int n = rnd.nextInt(2000)-1000; 
                    String s = n + "";            
                    bw.write(s); 
                    bw.newLine(); 
                }
                bw.close();
                fw.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            ArrayList<String> arrList = new ArrayList<>();
            try {
                FileReader fr = new FileReader("Test.txt");
                BufferedReader bf = new BufferedReader(fr);
                String st;
                while ((st = bf.readLine()) != null) {
                    arrList.add(st);
                }
                bf.close();
                fr.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //长度
            int len = arrList.size();
             BigInteger a[] = new BigInteger[len];
             BigInteger b[] = new BigInteger[20000000]; 
            for (int i = 0; i < len; i++) {
                //数组转移
                String s=arrList.get(i);
                a[i]=new BigInteger(String.valueOf(s));        
            }
            System.out.println(large(a, len)); // 打印最大子数组
        }
        private static BigInteger large(BigInteger[] a, int len) {
            // TODO Auto-generated method stub
            BigInteger max = a[0];
            BigInteger max2 =new BigInteger("0"); 
            for (int i = 0; i < len; i++) {
                max2 = max2.add(a[i]).max(a[0]); // 几个连续最大的值
                max = max.max (max2); // 目前为止最大值
            }
            return max;
        }
        private static int max(int a, int b) {
            if (a > b)
                return a;
            else
                return b;
        }
    }
    package zishuzu;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class Sum {
        public static boolean isInt(String val) {
            try {
            Integer.parseInt(val);
            return true;
            } catch (NumberFormatException e) {
            return false;
            }
            }
        public static void main(String[] args) throws IOException {
            try {
    
                FileWriter fw = new FileWriter("Test.txt");
                BufferedWriter bw = new BufferedWriter(fw);
                for (int i = 0; i < 10; i++) {
                    int n = (int) (Math.random() * 10);
                    String s = n + "";            
                    bw.write(s); 
                    bw.newLine(); 
                }
                bw.close();
                fw.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            ArrayList<String> arrList = new ArrayList<>();
            try {
                FileReader fr = new FileReader("Test.txt");
                BufferedReader bf = new BufferedReader(fr);
                String st;
                while ((st = bf.readLine()) != null) {
                    arrList.add(st);
                }
                bf.close();
                fr.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //长度
            int len = arrList.size();
            int a[] = new int[len];
            for (int i = 0; i < len; i++) {
                //数组转移
                String s=arrList.get(i);
                a[i] = Integer.parseInt(s);
            }
            System.out.println(large(a, len)); // 打印最大子数组
        }
        private static int max(int a, int b) {
            if (a > b)
                return a;
            else
                return b;
        }
        private static int large(int a[], int n) {
            int max = a[0];
            int max2 = 0;
            for (int i = 0; i < n; i++) {
                max2 = max(max2 + a[i], a[0]); // 几个连续最大的值
                max = max(max, max2); // 目前为止最大值
            }
            return max;
        }
    }
    package zishuzu;
    import java.util.Arrays;
    import java.util.Scanner;
    public class Sum {
     public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.print("输入数组个数:");
             int n = sc.nextInt();
             System.out.print("输入数组:");
             int a[] = new int[n*2];      
             for(int i=0; i<n; i++)           
                 {
              a[i] = sc.nextInt();
                 }
            
             for(int j=0;j<n-1;j++)
                {
                 a[n+j]=a[j];
                }
             sc.close();
             String intArrayString = Arrays.toString(a);
             System.out.println(large(a,n));  //打印最大子数组
     }
      private  static int max(int a,int b) {
       if(a>b) return a;
       else return b;
      }
        private  static int large(int a[],int n) {
         int max=a[0];
         int max2=0;    
         for (int i= 0; i<n*2; i++)
            {
            max2=max(max2+a[i],a[0]);   //几个连续最大的值
            max=max(max,max2);    
              }
         return max;
        }
    }

    代码如下:

  • 相关阅读:
    如何配置透明发光的骚气 vscode —— Jinkey 原创
    JS引用类型总结
    element UI -- 默认样式修改不成功原因
    上传本地Vue项目到github
    网页加载速度优化方法总结
    移动端click时间、touch事件、tap事件详解
    移动端开发用touch事件还是click事件
    禁止网站调用favicon.ico请求
    HTTP里面的响应和请求
    jave script 中this的指向 (六种场景)
  • 原文地址:https://www.cnblogs.com/zmh-980509/p/10504764.html
Copyright © 2011-2022 走看看