zoukankan      html  css  js  c++  java
  • 算法问题拓展——安全输入

    前情提要

      在计算子数组最大和的时候,我们一般只会给出默认情况来测试,数字是常见的,不会出问题。但如果我们是从文件中读取数据,那么就很可能出些奇怪的问题(多个空格?这还是正常的)。所以目前需要改进的就是安全输入问题。


      

    public int FindMaxSubArray(int[] a) {
            int n = a.length;
            int[] dp = new int[n];
            dp[0] = a[0];
            int sum = dp[0];
            
            for(int i = 1; i < n; i++){
                dp[i] = a[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);
                sum = Math.max(sum, dp[i]);
            }
            
            return sum;
    }
    原作者FujiwaranoSai

      核心运算采用动态划分的方法,这是最省内存运算最快的方法。

      然后为了支持大数,改写为BigInteger类

    public static BigInteger maxSubArray(ArrayList<BigInteger> A)
        {
            int n = A.size();
            BigInteger[] dp = new BigInteger[n];
            dp[0] = A.get(0);
            BigInteger max = dp[0];
    
            for (int i = 1; i < n; i++)
            {
                // dp[i] = A[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);
                // max = Math.max(max, dp[i]);
                if (dp[i - 1].compareTo(BigInteger.ZERO) == 1)
                {
                    dp[i] = A.get(i).add(dp[i - 1]);
                } else
                {
                    dp[i] = A.get(i);
                }
                if (max.compareTo(dp[i]) == -1)
                {
                    max = dp[i];
                }
            }
            return max;
        }

      文件输入时要注意的是——

    1. 数字之间的分割方式(但因为方便需要,我选择只有“ ”分割)
    2. 分割的数字能否转换为BigInterger
    3. 文件读取会不会出问题

      由此需要把异常提示处理好。

    public static ArrayList<BigInteger> fileReadIn(Path path)
        {
            RandomAccessFile writeFile = null;
            RandomAccessFile readFile = null;
            String split = " ";// TODO
            int number = 100000;// 随机数个数
            int digit = 256;// 随机数位数
            try
            {
                writeFile = new RandomAccessFile(path.toFile(), "rw");
                Random random = new Random();
                writeFile.setLength(0);// 清空文件
                for (int i = 0; i < number; i++)
                {
                    writeFile.writeBytes((new BigInteger(256, new Random())).toString());// 产生digit位的随机大数
                    if (i != number - 1)
                    {
                        writeFile.writeBytes(split);
                    }
                }
                writeFile.close();
    
                readFile = new RandomAccessFile(path.toFile(), "r");
                String fileString;
                ArrayList<BigInteger> arrayList = new ArrayList<>();
                while ((fileString = readFile.readLine()) != "" && fileString != null)
                {
    //                System.out.println(fileString);
                    for (String temp : fileString.split(split))
                    {
    
                        arrayList.add(new BigInteger(temp));
                    }
                    System.out.println(arrayList);
                }
                readFile.close();
                return arrayList;
                // return (BigInteger[]) arrayList.toArray(new BigInteger[arrayList.size()]);
            } catch (NumberFormatException e)
            {
                System.out.println("文件中有错误数字" + e.getMessage());
                e.printStackTrace();
            } catch (FileNotFoundException e)
            {
                System.out.println("未找到文件");
                e.printStackTrace();
            } catch (IOException e)
            {
                System.out.println("读取出现异常");
                e.printStackTrace();
            }
            return null;
        }

      我在文件输入前先产生了随机大数,以提供试验。

      结果很成功——

      

  • 相关阅读:
    SpringBoot第四集:整合JDBC和JPA(2020最新最易懂)
    SpringBoot第六集:整合监听器/过滤器和拦截器(2020最新最易懂)
    SpringBoot第八集:静态资源与首页定制(2020最新最易懂)
    TM1621断码液晶驱动IC的原理、驱动代码
    代码编码格式批量转换工具
    LAN8720 调试笔记
    C# 读取电脑CPU、主板、硬盘序列号等信息
    C# 读取串口设备列表
    C# 窗口全屏、置顶、获取焦点
    STM32 获取系统时钟频率
  • 原文地址:https://www.cnblogs.com/limitCM/p/10542016.html
Copyright © 2011-2022 走看看