最大子数组——回调

在之前的代码基础上增加新的功能,实现单步显示,数据回滚

使用switch语句进行输入判断。选择直接运行结束或者单步运行

单步执行中再加入switch进行选择选择回滚,并输入回滚位置,使用新的for循环获取回滚位置的数据,max1和sum1,并进行判断然后输出。

package shuzu;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        try{
            FileWriter fw =new FileWriter("a.txt");
        //随机数
        for(int i=0;i<100;i++) {
            long x=(long)(Math.random()*100)*(Math.random()>0.5?1:-1);
            String y=""+x;
            fw.write(y);
            fw.write("
");
           // System.out.println(y);
        }
        fw.close();}
        catch(FileNotFoundException e){
            e.printStackTrace();
        }

        ArrayList<String> arrList = new ArrayList<>();
        try {
            FileReader fr = new FileReader("a.txt");
            BufferedReader bf = new BufferedReader(fr);
            String st;
            while ((st = bf.readLine()) != null) {
                arrList.add(st);
            }
            bf.close();
            fr.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    long a[] = new long[arrList.size()];
    for (int i = 0; i <arrList.size(); i++) {
        //数组转移
        String s=arrList.get(i);
        a[i] = (long) Double.parseDouble(s);
        }
    
    long max=a[0];
    long sum=a[0];
    
    System.out.println("1.直接执行 2.单步执行");
    int m=sc.nextInt(); 
    switch(m) {
    case 1:{
        for(int i=1;i<a.length;i++) {
            if(sum<0)
                sum=a[i];
            else 
                sum+=a[i];
            if(sum>max)    max=sum;
            System.out.print("当前子数组和最大值为"+max);
            System.out.println("当前检查到第"+(i+1)+"位");  
            System.out.println();
        }
        System.out.println("最大子数组和:"+max);
        }break;
    case 2:
    {
        for(int i=1;i<=a.length;i++) {
            if(sum<0)
                sum=a[i];
            else 
                sum+=a[i];
            if(sum>max)    max=sum;
            System.out.print("当前子数组和最大值为"+max);
            System.out.println("当前检查到第"+i+"位");
            System.out.println();
            System.out.println("1.继续2.回滚");
            int n = sc.nextInt();  
            switch(n) {
            case 1:break;
            case 2:{
                 long max1=a[0];
                 long sum1=a[0];
                System.out.println("输入位数");
                int j=sc.nextInt();
                for(int k=1;k<=j;k++) {
                    if(sum1<0)
                        sum1=a[k];
                    else 
                        sum1+=a[k];
                    if(sum1>max1)    max1=sum1;
            }
                System.out.print("当前子数组和最大值为"+max1);
                System.out.println("当前检查到第"+j+"位");
            }break;
        }
        }System.out.println("最大子数组和:"+max);
    }
   }
  }
}
原文地址:https://www.cnblogs.com/lixv2018/p/10771137.html