一维数组4.25

实现对运算的控制

  1 package shuzu5;
  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 shuzu5 {
 10     static Scanner cin = new Scanner(System.in);    
 11     public static void readFileByLines(String fileName) {
 12         File file = new File(fileName);
 13         BufferedReader reader = null;
 14         try {
 15             reader = new BufferedReader(new FileReader(file));
 16             String tempString = null;           
 17 //            while ((tempString = reader.readLine()) != null) {
 18 //                System.out.println(tempString);                
 19 //            }
 20             reader.close();
 21         } catch (IOException e) {
 22             e.printStackTrace();
 23         } finally {
 24             if (reader != null) {
 25                 try {
 26                     reader.close();
 27                 } catch (IOException e1) {
 28                 }
 29             }
 30         }
 31     }
 32     
 33     public static long[] toArrayByFileReader1(String name) {
 34         // 使用ArrayList来存储每行读取到的字符串
 35         ArrayList<String> arrayList = new ArrayList<>();
 36         try {
 37             FileReader fr = new FileReader(name);
 38             BufferedReader bf = new BufferedReader(fr);
 39             String str;
 40             // 按行读取字符串
 41             while ((str = bf.readLine()) != null) {
 42                 arrayList.add(str);
 43             }
 44             bf.close();
 45             fr.close();
 46         } catch (IOException e) {
 47             e.printStackTrace();
 48         }
 49         // 对ArrayList中存储的字符串进行处理
 50         int length = arrayList.size();
 51         long[] array = new long[length];
 52         for (int i = 0; i < length; i++) {
 53             String s = arrayList.get(i);
 54             array[i] = Long.parseLong(s);
 55         }
 56         long[] newarray = new long[5*length];//剪开带子后新数组
 57         long[] result = new long[1000];//存放求和子数组
 58         int rs=0;//子数组计数器
 59         ArrayList<bean> blist = new ArrayList<bean>();
 60 //        for(int j = 0;j < array.length;j++)
 61 //        {              
 62 //                newarray[k++] = array[j];
 63 //                newarray[j+array.length] = array[j];
 64 //        } 
 65         int mlist,slist=0;
 66         long max;
 67         int i=0;
 68         int start=0;
 69         int end=0;
 70         String choice="y";
 71         while(choice.equals("y")&&i<length)        //O(n^2)求子数组之和
 72         {
 73             mlist = 0;
 74             int j = i;
 75             while(choice.equals("y")&&j<length)
 76             {
 77                 mlist +=array[j];
 78                 result[rs++] = mlist;        //将子数组之和存在数组result[]内
 79                 System.out.println("第"+ (slist+1) +"个子数组的和为:" + mlist);
 80                 slist++;
 81                 max = result[0];                            //将子数组和数组第一个值给max,然后循环进行比较,求出最大值
 82                 for(int kas = 0;kas<slist;kas++)
 83                 {
 84                     if(max < result[kas])
 85                         {max = result[kas];end=kas;start=i;}//i有问题
 86                 }
 87                 j++;
 88                 //将新数组存一下
 89                 bean bean=new bean(slist,mlist,max,start,end);
 90                 blist.add(bean);
 91                 System.out.println("当前数组的子数组之和的最大值为:"+max);
 92                 System.out.println("最大值从第"+(start+1)+"到第"+(end+1)+"位");
 93                 System.out.println("是否继续,继续则输入y;如果回退,输入b");
 94                 choice=cin.nextLine();
 95                 if(choice.equals("b")) {
 96                     System.out.println("回退到第几位?");
 97                     int nr = cin.nextInt();
 98                     if(nr>blist.size())
 99                         {
100                         System.out.println("输入有误!");
101                         }
102                     else {
103                     System.out.println("第"+ (blist.get((nr-1)).n) +"个子数组的和为:" + blist.get((nr-1)).sum);
104                     System.out.println("当前数组的子数组之和的最大值为:"+blist.get((nr-1)).max);
105                     System.out.println("最大值从第"+(blist.get((nr-1)).start+1)+"到第"+(blist.get((nr-1)).end+1)+"位");
106                     
107                     }
108                 }
109             }
110             i++;            
111         }
112         max = result[0];                            //将子数组和数组第一个值给max,然后循环进行比较,求出最大值
113         for(int h = 0;h<slist;h++)
114         {
115             if(max < result[h])
116                 max = result[h];
117         }
118         //将新数组存一下        
119         System.out.println("该数组的子数组之和的最大值为:"+max);      
120         // 返回数组
121         return array;
122     }   
123 
124 
125     public static void main(String[] args) throws IOException{      
126         String name = new String("E:\Program Files\eclipse操作\shuzu\src\test2\input.txt");       
127         readFileByLines(name);
128         toArrayByFileReader1(name);//文件路径       
129     }
130 }

只是没有实现会退后循环,目前是回退之后便结束,还有待提升

原文地址:https://www.cnblogs.com/flw0322/p/10771717.html