• 求数组的子数组之和的最大值III(循环数组)


    新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试

    想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存到result数组中,最后比较result数组的最大值

    代码如下:

      1 package test2;
      2 import java.io.BufferedReader;
      3 import java.io.File;
      4 import java.io.FileReader;
      5 import java.io.IOException;
      6 import java.util.ArrayList;
      7 import java.util.Scanner;
      8  
      9 public class shuzu2 {
     10     
     11     static Scanner cin = new Scanner(System.in);
     12     
     13     public static void readFileByLines(String fileName) {
     14         File file = new File(fileName);
     15         BufferedReader reader = null;
     16         try {
     17             reader = new BufferedReader(new FileReader(file));
     18             String tempString = null;
     19            
     20             while ((tempString = reader.readLine()) != null) {
     21 
     22                 System.out.println(tempString);
     23                 
     24             }
     25             reader.close();
     26         } catch (IOException e) {
     27             e.printStackTrace();
     28         } finally {
     29             if (reader != null) {
     30                 try {
     31                     reader.close();
     32                 } catch (IOException e1) {
     33                 }
     34             }
     35         }
     36     }
     37     
     38     public static long[] toArrayByFileReader1(String name) {
     39         // 使用ArrayList来存储每行读取到的字符串
     40         ArrayList<String> arrayList = new ArrayList<>();
     41         try {
     42             FileReader fr = new FileReader(name);
     43             BufferedReader bf = new BufferedReader(fr);
     44             String str;
     45             // 按行读取字符串
     46             while ((str = bf.readLine()) != null) {
     47                 arrayList.add(str);
     48             }
     49             bf.close();
     50             fr.close();
     51         } catch (IOException e) {
     52             e.printStackTrace();
     53         }
     54         // 对ArrayList中存储的字符串进行处理
     55         int length = arrayList.size();
     56         long[] array = new long[length];
     57         for (int i = 0; i < length; i++) {
     58             String s = arrayList.get(i);
     59             array[i] = Long.parseLong(s);
     60         }
     61         long[] newarray = new long[5*length];//剪开带子后新数组
     62         long[] result = new long[1000];//存放求和子数组
     63         int rs=0;//子数组计数器
     64         long f=0;//定义整形变量f,为子数组最大值
     65         long sum=0;//定义整形变量sum,为子数组求和
     66         System.out.println("输入遍历开始的位置:");
     67         int a = cin.nextInt();    
     68         int sa = 0;//a用来存储剪开带子位置
     69         int k = 0 ;
     70         long []jieguo = new long[1000];
     71         for(int j = 0;j < array.length;j++)
     72         {              
     73                 newarray[k++] = array[j];
     74                 newarray[j+array.length] = array[j];
     75                 //System.out.println("1   "+newarray[j]);
     76         } 
     77         for(int i=0;i<2*array.length;i++)
     78         System.out.println("2    "+newarray[i]);
     79         int mlist,slist=0;
     80         long max;
     81         for(int i=0;i<length;i++)                        //O(n^2)求子数组之和
     82         {
     83             mlist = 0;
     84             for(int j=i;j<length+i;j++)
     85             {
     86                 mlist +=newarray[j];
     87                 result[rs++] = mlist;        //将子数组之和存在数组result[]内
     88                 System.out.println("第"+ (slist+1) +"个子数组的和为:" + mlist);
     89                 slist++;
     90             }
     91         }
     92         for(int i=0;i<rs;i++)
     93         {System.out.println("reslut"+i+"  "+result[i]);}
     94         max = result[0];                            //将子数组和数组第一个值给max,然后循环进行比较,求出最大值
     95         for(int i = 0;i<slist;i++)
     96         {
     97             if(max < result[i])
     98                 max = result[i];
     99         }
    100         //将新数组存一下        
    101         System.out.println("该数组的子数组之和的最大值为:"+max);      
    102         // 返回数组
    103         return array;
    104     }
    105 
    106     
    107     public static void main(String[] args) throws IOException{
    108        
    109         String name = new String("E:\Program Files\eclipse操作\shuzu\src\test2\input.txt");
    110         
    111         readFileByLines(name);
    112         toArrayByFileReader1(name);//文件路径
    113        
    114     }
    115 }

    运行结果:

    遇到的问题:在这次处理中,数组下标越界的情况比较多,考虑情况比较少,课上想用的是数据结构中学的循环队列来解决,但是没有实现,算法还得好好复习复习。

  • 相关阅读:
    PHP-redis中文文档
    非关系型数据库Redis学习(3)
    非关系型数据库Redis学习(1)
    【Android】找不到类
    【leetcode】删除单向链表的节点
    【上海交大oj】数学题3(数位dp)
    【上海交大oj】括号匹配加强 (动态规划)
    【上海交大oj】纸来纸去(动态规划)
    【上海交大oj】1053 二哥的内存
    【上海交大oj】畅畅的牙签袋(改)(枚举+模拟)
  • 原文地址:https://www.cnblogs.com/flw0322/p/10609966.html
Copyright © 2020-2023  润新知