zoukankan      html  css  js  c++  java
  • 子数组最大求和(续)

      这次实验的主要设计思路和上次的大框差不多,只是不同的地方就是从文件获取大量的数据和对大型数据的处理,我主要是将文件中的内容读取成了double型,进而使其能处理比较大的数据,但要是想让其处理更大的数据只能将其转化为big型。

    package shuzu;
    
    import java.io.*;
    import java.util.*;
    
    public class shuzu {
        public static void main(String[] args) throws IOException{
            
            String path = "F:\JAVA_2019\shuzu\shuzu.txt";
              double[] nums = writeToDat(path);
              int[] shuzu= new int[nums.length];
              for(int i=0;i<nums.length;i++){
               shuzu[i]=(int)nums[i];
              }
            //时间复杂度没有实现,只是实现了输出最大值
            int nshu;//循环的次数
            int shu[] = {-1,3,-6,-5,-2}; 
            int max = (int)shuzu[0];//存储最大的和
            for(nshu=0;nshu<nums.length;nshu++) {
                int n1 = 0;
                int n2 = 0;
                for(int nnshu=nshu;nnshu<nums.length;nnshu++) {
                    n1 = n1 + shuzu[nnshu];
                    if(nnshu<4) {
                        nnshu = nnshu + 1;
                        n2 = n1 + shuzu[nnshu];
                        max = maxxx(n1,n2,max);
                        nnshu = nnshu - 1;
                    }else {
                        max = maxx(n1,max);
                    }
                }
            }
            System.out.println("最大值" + max);
            System.exit(0);
        }
        
        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;
        }
        
        public static double[] writeToDat(String path) {
              File file = new File(path);
              List list = new ArrayList();
              double[] nums = null;
              try {
               BufferedReader bw = new BufferedReader(new FileReader(file));
               String line = null;
               //因为不知道有几行数据,所以先存入list集合中
               while((line = bw.readLine()) != null){
                list.add(line);
               }
               bw.close();
              } catch (IOException e) {
               e.printStackTrace();
              }
              //确定数组长度
              nums = new double[list.size()];
              for(int i=0;i<list.size();i++){
               String s = (String) list.get(i);
               nums[i] = Double.parseDouble(s);
              }
              return nums;
             }
    }

    最后想说的就是虽然能勉强实现处理大数的功能但是对大量数据的处理还没有到理想化,所以还要在优化上下功夫。

  • 相关阅读:
    后缀零
    vs2019 MFC的安装 解决 此项目需要 MFC 库。从 Visual Studio 安装程序(单个组件选项卡)为正在使用的任何工具集和体系结构安装它们。
    矩形切割
    fsync: failed to connect to feed.openvas.org (89.146.224.58)
    How to Install GVM Trial With VMware Workstation Player/Pro
    GVM 21.04 (stable, initial release 20210416)
    gitlab 升级相关
    Adobe Reader XI 11.0.23 简体中文版
    gofileserver GO HTTP 文件下载服务器
    KVM 中安装 Windows 10 虚拟机、CentOS 8 虚拟机及快照操作
  • 原文地址:https://www.cnblogs.com/hwh000/p/10545328.html
Copyright © 2011-2022 走看看