• 关于求已知整数数组的连续子数组的最大和的方法 ——基于软件工程的要求给予优化


    日期:2019.3.15 

    博客期:047

    星期五

      上一回我们知道将数组的连续子数组的最大和的求法,我们知道应该对它以软件工程的思想进行优化,也就是bug修复和用户体验优化等(因为这两部分是今天要实现的,所以要提一下)。

      我给出的优化是:

      1、将数据改为从文件读入,免去用户控制台输入的操作

      2、将文件的数据内容改为随机数存储,这样用户也可以不用管理数据。

      3、处理一系列的错误抛出,如:读入Integer的范围、算法计算过程中的Integer范围(这个是老师提醒的,之前没有注意到,今后要加倍注意)、文件不存在、文件内部没有数据、文件数据中存在非数字的内容。

      4、超过Integer范围的数字需要保留下来

      5、实现更多功能?不仅仅是最大值?

      

      所以,进行了修理:

      (一)、将数据改为从文件读入,免去用户控制台输入的操作

        复习与文件相关的知识点:

        File PrintWriter Scanner

      (二)、将文件的数据内容改为随机数存储,这样用户也可以不用管理数据。

         复习生成随机数的相关知识点:

        Random System

      (三)、处理一系列的错误抛出,如:读入Integer的范围、算法计算过程中的Integer范围(这个是老师提醒的,之前没有注意到,今后要加倍注意)、文件不存在、文件内部没有数据、文件数据中存在非数字的内容。

         使用try catch语句处理Exception

      (四)、超过Integer范围的数字需要保留下来

         处理Number相关Exception

      (五)、实现更多功能?不仅仅是最大值?

        比如再求出数组是从第i个开始的,到哪里结束的。

      

      

      

      

      1 package pvp;
      2 
      3 import java.io.File;
      4 import java.io.FileNotFoundException;
      5 import java.math.BigInteger;
      6 import java.util.Scanner;
      7 
      8 public class TestPlay {
      9     public static void main(String[] args) {
     10         //AddRandomNumber.Renew2();
     11         File f = new File("data/data.txt");
     12         if(!f.exists())
     13         {
     14             System.out.println("文件不存在!");
     15             System.exit(0);
     16         }
     17         //存储导入内容
     18         String str;
     19         //内容
     20         Scanner sc = null;
     21         try {
     22             sc = new Scanner(f);
     23         } catch (FileNotFoundException e1) {
     24             System.out.println("文件不存在!");
     25             System.exit(0);
     26         }
     27         //最大值
     28         BigInteger rmax = new BigInteger("0");
     29         //正数总值
     30         BigInteger Tnum = new BigInteger("0");
     31         //负数总值
     32         BigInteger Fnum = new BigInteger("0");
     33         //记录是否发生转变
     34         int sis = 0;
     35         //标记是第几程度
     36         int attitude = 0;
     37         //循环
     38         try
     39         {
     40             if(!sc.hasNext())
     41             {
     42                 System.out.println("文件内容为空!");
     43                 System.exit(0);
     44             }
     45             while(sc.hasNext())
     46             {
     47                 str = sc.next();
     48                 BigInteger p = new BigInteger(str);
     49                 if(attitude==0)                    //---------------------------------------[寻找第一个正数]
     50                 {
     51                     if(p.compareTo(new BigInteger("0"))<=0)
     52                         ;
     53                     else
     54                     {
     55                         Tnum = p;
     56                         attitude = 1;
     57                     }
     58                 }
     59                 else if(attitude==1)            //---------------------------------------[上一个数为正数]
     60                 {
     61                     if(p.compareTo(new BigInteger("0"))<0)
     62                     {
     63                         if(sis==0)
     64                         {
     65                             sis = 1;
     66                             Fnum.add(p);
     67                         }
     68                         else
     69                             Fnum = p;
     70                         attitude = -1;
     71                     }
     72                     else
     73                         Tnum = Tnum.add(p);
     74 
     75                     if(Tnum.compareTo(rmax)>0)
     76                         rmax = Tnum;
     77                 }
     78                 else                            //---------------------------------------[上一个数为负数]
     79                 {
     80                     if(p.compareTo(new BigInteger("0"))>0)
     81                     {
     82                         attitude = 1;
     83                         if(Tnum.compareTo(new BigInteger("0").subtract(Fnum))>0)
     84                         {
     85                             
     86                             Tnum = Tnum.add(Fnum).add(p);
     87                         }
     88                         else
     89                             Tnum = p;
     90                     }
     91                     else
     92                     {
     93                         Fnum = Fnum.add(p);
     94                     }
     95                 }
     96                 if(p.compareTo(rmax)>0)
     97                     rmax = p;
     98                 if(p.compareTo(Tnum)>0)
     99                     rmax = Tnum;
    100             }
    101         }
    102         catch( NumberFormatException e){
    103             System.out.println("输入内容不是数字!");
    104             System.exit(0);
    105         }
    106         System.out.println(rmax);
    107         sc.close();
    108     }
    109 }
    TextPlay.java
      1 package pvp;
      2 
      3 import java.io.File;
      4 import java.io.FileNotFoundException;
      5 import java.util.Scanner;
      6 
      7 public class TestPlay2 {
      8     public static void main(String[] args) {
      9         AddRandomNumber.Renew();
     10         File f = new File("data/data.txt");
     11         if(!f.exists())
     12         {
     13             System.out.println("文件不存在!");
     14             System.exit(0);
     15         }
     16         //存储导入内容
     17         String str;
     18         //内容
     19         Scanner sc = null;
     20         try {
     21             sc = new Scanner(f);
     22         } catch (FileNotFoundException e1) {
     23             System.out.println("文件不存在!");
     24             System.exit(0);
     25         }
     26         //最大值
     27         int rmax = Integer.MIN_VALUE;
     28         //正数总值
     29         int Tnum = Integer.MAX_VALUE;
     30         //负数总值
     31         int Fnum = 0;
     32         //记录是否发生转变
     33         int sis = 0;
     34         //标记是第几程度
     35         int attitude = 0;
     36         //循环
     37         try
     38         {
     39             if(!sc.hasNext())
     40             {
     41                 System.out.println("文件内容为空!");
     42                 System.exit(0);
     43             }
     44             while(sc.hasNext())
     45             {
     46                 int p;
     47                 str = sc.next();
     48                 p = Integer.parseInt(str);
     49                 if(attitude==0)                    //---------------------------------------[寻找第一个正数]
     50                 {
     51                     if(p<=0)
     52                         ;
     53                     else
     54                     {
     55                         Tnum = p;
     56                         attitude = 1;
     57                     }
     58                 }
     59                 else if(attitude==1)            //---------------------------------------[上一个数为正数]
     60                 {
     61                     if(p<0)
     62                     {
     63                         if(sis==0)
     64                         {
     65                             sis = 1;
     66                             Fnum += p;
     67                         }
     68                         else
     69                             Fnum = p;
     70                         attitude = -1;
     71                     }
     72                     else
     73                         Tnum += p;
     74 
     75                     if(Tnum>rmax)
     76                         rmax = Tnum;
     77                 }
     78                 else                            //---------------------------------------[上一个数为负数]
     79                 {
     80                     if(p>0)
     81                     {
     82                         attitude = 1;
     83                         if(Tnum + Fnum > 0)
     84                         {
     85                             if(Tnum + Fnum > Integer.MAX_VALUE - p)
     86                             {
     87                                 System.out.println("统计内容超过最大值!");
     88                                 System.exit(0);
     89                             }
     90                             Tnum = (Tnum + Fnum) + p;
     91                         }
     92                         else
     93                             Tnum = p;
     94                     }
     95                     else
     96                     {
     97                         if(Fnum<Integer.MIN_VALUE-p)
     98                         {
     99                             System.out.println("统计内容超过最小值!");
    100                             System.exit(0);
    101                         }
    102                         Fnum += p;
    103                     }
    104                 }
    105                 if(p>rmax)
    106                     rmax = p;
    107                 if(Tnum>rmax)
    108                     rmax = Tnum;
    109                 
    110             }
    111         }
    112         catch( NumberFormatException e){
    113             System.out.println("输入内容不是数字或者过大!");
    114             System.exit(0);
    115         }
    116         System.out.println(rmax);
    117         sc.close();
    118     }
    119 }
    TextPlay2.java
     1 package pvp;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.IOException;
     6 import java.io.PrintWriter;
     7 import java.util.Random;
     8 
     9 public class AddRandomNumber {
    10     public static void main(String[] args) {
    11         System.out.println(Integer.MAX_VALUE);
    12         System.out.println(Integer.MIN_VALUE);
    13     }
    14     public static void Renew2(){
    15         File f = new File("data/data.txt");
    16         if(!f.exists())
    17         {
    18             try {
    19                 f.createNewFile();
    20             } catch (IOException e) {
    21                 //Do Nothing...
    22             }
    23         }
    24         PrintWriter pw = null;
    25         try {
    26             pw = new PrintWriter(f);
    27         } catch (FileNotFoundException e) {
    28             System.out.println("文件不存在!");
    29             System.exit(0);
    30         }
    31         Random ra = new Random(System.currentTimeMillis());
    32         for(long i=0;i<1437824721;++i)
    33         {
    34             long p = ra.nextLong();
    35             
    36             pw.println(p);
    37         }
    38         pw.close();
    39     }
    40     public static void Renew(){
    41         File f = new File("data/data.txt");
    42         if(!f.exists())
    43         {
    44             try {
    45                 f.createNewFile();
    46             } catch (IOException e) {
    47                 //Do Nothing...
    48             }
    49         }
    50         PrintWriter pw = null;
    51         try {
    52             pw = new PrintWriter(f);
    53         } catch (FileNotFoundException e) {
    54             System.out.println("文件不存在!");
    55             System.exit(0);
    56         }
    57         Random ra = new Random(System.currentTimeMillis());
    58         for(int i=0;i<4378247;++i)
    59         {
    60             int p = ra.nextInt();
    61             p = p %100;
    62             p = p + 50;
    63             pw.println(p);
    64         }
    65         pw.close();
    66     }
    67 }
    AddRandomNumber.java
  • 相关阅读:
    fiddler过滤css、js、jpg等文件。
    利用jmeter批量创建测试数据(一)---csv data set config
    【Qt】实现程序重启的两种方法
    【MySQL 基础】MySQL必知必会
    【设计模式】访问者模式
    【设计模式】状态模式
    【设计模式】观察者模式
    【设计模式】备忘录模式
    【设计模式】中介者模式
    【设计模式】迭代器模式
  • 原文地址:https://www.cnblogs.com/onepersonwholive/p/10534748.html
Copyright © 2020-2023  润新知