zoukankan      html  css  js  c++  java
  • 一位数组续

    设计并实现一个系统,让一个普通用户就能通过单步执行的方式看到你的算法是如何工作的。

    1.1用户也可以自行定义数组的大小

    2.1按单步执行键,在GUI看到你的程序是如何一步一步算出目前最大子数组的范围,当前计算到的临时子数组是在哪里,等等。最好用不同的颜色标识不同的状态。

    2.2按自动运行键,在GUI看到程序自动运行,并自动显示过程,每次改变状态的时候要稍作停留

    3.最好有一个倒带/回滚的键,让用户可以看清楚关键的几步

    设计思路:
    设计一个字符串数组来储存当前最大子数组,设置两个变量来计算当前最大子数组的位置。之后创建一个函数来实现功能

    代码:

    import java.util.Scanner;
    
    
    /*
     * 求子数组的最大值
     */
    public class shuzu {
        static int n;
        static String[] Result = new String[100];
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            int num[] = new int[100];
            Scanner in = new Scanner(System.in);
            
            
            int start,end = 0;
            int a=0;
            
            int sum = 0;
            int value = 0;
            
            System.out.println("输入数组的个数");
            n = in.nextInt();
            System.out.println("输入数组中的值");
            for(int i = 0;i < n;i++)
            {
                num[i] = in.nextInt();
            }
            start = 0;
            sum = num[0];
            
            
            
            for(int i = 0;i < n;i++)
            {
                if (value <= 0) {
                    value = num[i]; 
                    start = i;
                    end ++;
                    a++;
                }else {
                    value += num[i]; 
                    end ++;
                    
                }
                
                
                
                if (sum < value) {    
                    sum = value;
                    Result[i] = "第" + (i+1) + "步:" +"此时的最大子数组为" + sum + "从"+ (start+1) + "到" + end  + "已经计算了"  + a + "个子数组" ;
                    
                }else {
                    Result[i] = "第" + (i+1) + "步:" +"此时的最大子数组为" + sum + "从"+ (start+1) + "到" + end  +"已经计算了"  + a + "个子数组";
    
                }
                
            }
            done();
            System.out.println("最大值为" + sum);
            
        }
        
        public static void done()
        {
            int stop,k;
            Scanner in = new Scanner(System.in);
            System.out.println("单步调试请按1");
            stop = in.nextInt();
            if (stop != 1) {
                for(int i = 0 ; i < n ; i++)
                {
                    System.out.println(Result[i]);
                }
            }
            else {
                for(int i = 0 ; i < n ; i++)
                {
                    System.out.println("继续执行请按1");
                    stop = in.nextInt();
                    if (stop == 1) {
                        System.out.println(Result[i]);
                        continue;
                    }
                    
                }
            }
            int choice = 0;
            
            
            while(choice != 1)
            {
                System.out.println("是否需要回滚,回滚请按0");
                choice = in.nextInt();
                if (choice == 0) {
                    System.out.println("请输入需要回滚到的步数");
                    k = in.nextInt();
                    for(int t=k-1;t<n;t++)
                    {
                    System.out.println(Result[t]);
                    }
                }
            }
            
        }
        
    
    }

    实验截图:

  • 相关阅读:
    安装配置ssh免密码登录
    大数据学习之Linux环境搭建(导航)
    Linux下搭建sqli-labs环境
    SpringMVC freemarker 中 Could not resolve view with name 'XXX.ftl' in servlet with name 'SpringMVC'
    配置FreeMarker时IDEA提示cannot resolve property 'templateLoaderPath'
    MySQL在指定字段后添加一个新字段
    META-INF/MANIFEST.MF file not found in unnamed.war
    Java获取音频播放时长
    JS实现阿拉伯数字转韩文
    微信公众号开发-素材管理-调用接口返回结果一览表
  • 原文地址:https://www.cnblogs.com/liujinxin123/p/10770355.html
Copyright © 2011-2022 走看看