软件工程第三周课上练习

  1 package 数组;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileWriter;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 import java.util.Scanner;
 12 
 13 public class Shuzu7 {
 14 public static void main(String[] args)throws IOException
 15 {
 16 List<Integer> list=new ArrayList<>();
 17 Scanner scan=new Scanner(System.in);
 18 System.out.println("请执行操作:1、普通数组求最大子数组       2、百万数组求最大子数组");
 19 int x=scan.nextInt();
 20 if(x==1)
 21 {
 22  try {
 23     File file = new File("shuzu.txt");
 24     if(file.isFile() && file.exists()) {
 25       InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
 26       BufferedReader br = new BufferedReader(isr);
 27       String lineTxt = "";
 28       while ((lineTxt = br.readLine()) != null){
 29        list.add(Integer.parseInt(lineTxt));
 30       }
 31       Object[]a=list.toArray();
 32       System.out.println((int)a[1]+(int)a[3]);
 33       br.close();
 34  int sum=a.length;
 35  int sum1=0;
 36 for(int i=1;i<=sum;i++)
 37 {
 38 sum1=sum1+i;
 39 }
 40 int [] b=new int[sum1];
 41 int temp1=0;
 42 for(int i=0;i<sum;i++)
 43 {
 44 int temp=0;
 45 for(int k=0;k<sum-i;k++)
 46 {   
 47     temp=temp+(int)a[i+k];
 48 b[temp1]=temp;
 49 temp1++;
 50 }
 51 }
 52 for(int i=0;i<sum1-1;i++)
 53 for(int k=0;k<sum1-1-i;k++)
 54 {
 55 if(b[k]<b[k+1])
 56 {
 57 int temp2=b[k];
 58 b[k]=b[k+1];
 59 b[k+1]=temp2;
 60 }
 61 }
 62 System.out.println("最大的子数组和为"+b[0]);
 63     } else {
 64       System.out.println("文件不存在!");
 65     }
 66   } catch (Exception e) {
 67     System.out.println("文件读取错误!");
 68   }
 69 }
 70 if(x==2)
 71 {
 72 FileWriter fw = null;
 73   try {
 74             //创建字符输出流
 75             fw = new FileWriter("shuzu1.txt");
 76             for(int i=0;i<50;i++)
 77             {
 78                 int intval=(int)(Math.random()*-9000000+8999999);
 79             fw.write(""+intval+"\r\n");
 80             }
 81         } catch (IOException ioe) {
 82             ioe.printStackTrace();
 83         } finally {
 84             //使用finally块来关闭文件输出流
 85             if (fw != null) {
 86                 fw.close();
 87             }
 88         }
 89   try {
 90     File file = new File("shuzu1.txt");
 91     if(file.isFile() && file.exists()) {
 92       InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
 93       BufferedReader br = new BufferedReader(isr);
 94       String lineTxt = "";
 95       while ((lineTxt = br.readLine()) != null){
 96        list.add(Integer.parseInt(lineTxt));
 97       }
 98       Object[]a=list.toArray();
 99       System.out.println((int)a[1]+(int)a[3]);
100       br.close();
101  int sum=a.length;
102  int sum1=0;
103 for(int i=1;i<=sum;i++)
104 {
105 sum1=sum1+i;
106 }
107 int [] b=new int[sum1];
108 int temp1=0;
109 for(int i=0;i<sum;i++)
110 {
111 int temp=0;
112 for(int k=0;k<sum-i;k++)
113 {   
114     temp=temp+(int)a[i+k];
115 b[temp1]=temp;
116 temp1++;
117 }
118 }
119 for(int i=0;i<sum1-1;i++)
120 for(int k=0;k<sum1-1-i;k++)
121 {
122 if(b[k]<b[k+1])
123 {
124 int temp2=b[k];
125 b[k]=b[k+1];
126 b[k+1]=temp2;
127 }
128 }
129 System.out.println("最大的子数组和为"+b[0]);
130     } else {
131       System.out.println("文件不存在!");
132     }
133   } catch (Exception e) {
134     System.out.println("文件读取错误!");
135   }
136 }
137   }
138 
139 }

 

posted on 2019-03-17 15:31  小朝~~~  阅读(173)  评论(0编辑  收藏  举报

导航