• 返回一个整型数组中最大子数组的和(2)


    1.题目1:返回一个整数数组中最大子数组的和。

    要求:

    输入一个整形数组,数组里有正数也有负数。

    数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。

    求所有子数组的和的最大值。要求时间复杂度为O(n)。

    新情况:

    要求数组从文件读取。

    如果输入的数组很大, 并且有很多大的数字, 就会产生比较大的结果 (考虑一下数的溢出), 请保证你的程序能正常输出。

    另外, 如果输入文件的参数有错误, 这个程序应该能正常退出, 并显示相应的错误信息。 任何输入错误都不能导致你的程序崩溃。

    程序源代码:

     1 import java.io.BufferedReader;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileReader;
     5 import java.io.IOException;
     6 import java.util.ArrayList;
     7 import java.util.HashMap;
     8 import java.util.List;
     9 import java.util.Map;
    10 public class file {
    11   public static void main(String[] args) throws IOException {
    12       int num=0;
    13       int p[]=new int[1000000];
    14       BufferedReader reader = new BufferedReader(new FileReader(
    15               "c:\新建文件夹\\2020.2.25.txt"));
    16       StringBuffer buffer = new StringBuffer();
    17       String line = null;
    18       while ((line = reader.readLine()) != null) {
    19           buffer.append(line);
    20       }
    21     reader.close();
    22       String str = buffer.toString();
    23       str = str.replace(',', ' ');//将逗号用空格替换
    24       str = str.replace('.', ' ');//将句号用空格替换
    25 //      如果有其他参数,替换就好
    26 
    27       String[] strings = str.split("\s+");   // “\s+”代表一个或多个空格,是正则表达式
    28       int array[]=new int[strings.length];
    29       for(int i=0;i<strings.length;i++) {
    30        array[i]=Integer.parseInt(strings[i]);
    31       }
    32     int max=0;
    33 //  m数组是存放每一轮子数组最大值
    34   int m[]=new int[strings.length];
    35   for(int j=0;j<strings.length;j++)
    36   {
    37       max=array[j];
    38       int sum=0;
    39       for(int t=j;t<strings.length;t++)
    40       {
    41           if(sum>Integer.MAX_VALUE-array[t]||sum<Integer.MIN_VALUE+array[t])//防止数据溢出
    42           {System.out.println("溢出");break;
    43           }else {
    44           sum=sum+array[t];
    45           }
    46           if(sum>max)
    47           {
    48               max=sum;
    49           }
    50       }
    51       m[j]=max;
    52   }
    53   for(int i=0;i<strings.length;i++)
    54   {
    55       System.out.print("第"+(i+1)+"次比较的子数组的和的最大值为:");
    56       System.out.println(m[i]);
    57   }
    58   for(int i=1;i<strings.length;i++)
    59   {
    60       max=m[0];
    61       if(m[i]>max)
    62       {
    63           max=m[i];
    64       }
    65   }
    66   System.out.print("子数组和的最大值为:"+max);
    67   }
    68 }

    运行结果截图:

     

  • 相关阅读:
    CentOS下date命令
    spring-data-redis --简单的用spring-data-redis
    Unable to Rebuild JIRA Index
    JIRA Cannot Start Due to 'unable to clean the cache directory: /opt/jira/plugins/.osgi-plugins/felix'
    Java compiler level does not match the version of the installed Java project facet.
    maven scope含义的说明
    maven 下载 源码和javadoc命令
    Redis 入门第一发
    mysql 1194 – Table ‘tbl_video_info’ is marked as crashed and should be repaired 解决方法
    tomcat用redis做session共享
  • 原文地址:https://www.cnblogs.com/a155-/p/12364195.html
Copyright © 2020-2023  润新知