软件工程第三周课堂作业

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class shuzu2 {
public static void main(String[] args)throws IOException
{
List<Integer> list=new ArrayList<>();
Scanner scan=new Scanner(System.in);
System.out.println("请执行操作:1、普通数组求最大子数组       2、百万数组求最大子数组");
int x=scan.nextInt();
if(x==1)
{
 try {
    File file = new File("shuzu.txt");
    if(file.isFile() && file.exists()) {
      InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
      BufferedReader br = new BufferedReader(isr);
      String lineTxt = "";
      while ((lineTxt = br.readLine()) != null){
       list.add(Integer.parseInt(lineTxt));
      }
      Object[]a=list.toArray();
      System.out.println((int)a[1]+(int)a[3]);
      br.close();
 int sum=a.length;
 int sum1=0;
for(int i=1;i<=sum;i++)
{
sum1=sum1+i;
}
int [] b=new int[sum1];
int temp1=0;
for(int i=0;i<sum;i++)
{
int temp=0;
for(int k=0;k<sum-i;k++)
{   
    temp=temp+(int)a[i+k];
b[temp1]=temp;
temp1++;
}
}
for(int i=0;i<sum1-1;i++)
for(int k=0;k<sum1-1-i;k++)
{
if(b[k]<b[k+1])
{
int temp2=b[k];
b[k]=b[k+1];
b[k+1]=temp2;
}
}
System.out.println("最大的子数组和为"+b[0]);
    } else {
      System.out.println("文件不存在!");
    }
  } catch (Exception e) {
    System.out.println("文件读取错误!");
  }
}
if(x==2)
{
FileWriter fw = null;
  try {
            //创建字符输出流
            fw = new FileWriter("shuzu1.txt");
            for(int i=0;i<50;i++)
            {
                int intval=(int)(Math.random()*-9000000+8999999);
            fw.write(""+intval+"\r\n");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            //使用finally块来关闭文件输出流
            if (fw != null) {
                fw.close();
            }
        }
  try {
    File file = new File("shuzu1.txt");
    if(file.isFile() && file.exists()) {
      InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
      BufferedReader br = new BufferedReader(isr);
      String lineTxt = "";
      while ((lineTxt = br.readLine()) != null){
       list.add(Integer.parseInt(lineTxt));
      }
      Object[]a=list.toArray();
      System.out.println((int)a[1]+(int)a[3]);
      br.close();
 int sum=a.length;
 int sum1=0;
for(int i=1;i<=sum;i++)
{
sum1=sum1+i;
}
int [] b=new int[sum1];
int temp1=0;
for(int i=0;i<sum;i++)
{
int temp=0;
for(int k=0;k<sum-i;k++)
{   
    temp=temp+(int)a[i+k];
b[temp1]=temp;
temp1++;
}
}
for(int i=0;i<sum1-1;i++)
for(int k=0;k<sum1-1-i;k++)
{
if(b[k]<b[k+1])
{
int temp2=b[k];
b[k]=b[k+1];
b[k+1]=temp2;
}
}
System.out.println("最大的子数组和为"+b[0]);
    } else {
      System.out.println("文件不存在!");
    }
  } catch (Exception e) {
    System.out.println("文件读取错误!");
  }
}
  }

}

 

猜你喜欢

转载自www.cnblogs.com/lover995/p/10549074.html
0条评论
添加一条新回复
  
今日推荐