image.png


    1. package ActualCombat;
    2. import java.util.Iterator;
    3. public class test {
    4. public static void main(String[] args) {
    5. int s = 0,w = 0;
    6. int numbers[] = {1, -2, 3, 10, -4, 7, 2, -5}; //创建原数组
    7. int[] memory = new int[100]; //备忘录
    8. int[] maxnub = new int[numbers.length]; //存储最大值
    9. int[] end = new int[numbers.length];//子序列最后的位置
    10. for (int i = 0; i < numbers.length; i++) {
    11. maxnub[i] = numbers[i];
    12. memory[i] = numbers[i];
    13. for (int j = i+1; j < numbers.length; j++) {
    14. memory[j] = memory[j-1]+numbers[j];
    15. if (memory[j] > maxnub[i]) {
    16. maxnub[i] = memory[j];
    17. end[i] = j;
    18. }
    19. }
    20. for (int k = 0; k <= numbers.length; k++) {
    21. memory[k] = 0;
    22. }
    23. }
    24. for (int i = 0; i < numbers.length; i++) {
    25. System.out.println(maxnub[i]);
    26. }
    27. int max = maxnub[0];
    28. for (int i = 1; i < maxnub.length; i++) { //计算出最终的最大值,以及确定子序列
    29. if (maxnub[i] > max) {
    30. max = maxnub[i];
    31. s = i;
    32. w = end[i];
    33. }
    34. }
    35. System.out.println("最大连续子序列和为:"+max);
    36. System.out.println("子序列内容如下:");
    37. for (int i = s; i <= w; i++) {
    38. System.out.println(numbers[i]);
    39. }
    40. }
    41. }

    image.png
    但是我写的代码还有不足之处,在处理新数组array[1, 4, -5, 9, 8, 3, -6]出现错误
    image.png
    出现错误的不是最大值计算,而是子序列的确定
    查看Dubug情况
    image.png
    end存储的结果没有任何问题
    问题出在
    image.png
    所以我们添加一行代码 w = end[0];
    最终代码为

    package com.xy.recursion;
    
    public class xisy {
    
        public static void main(String[] args) {
            int s = 0,w = 0;
            int numbers[] = {1, 4, -5, 9, 8, 3, -6};    //创建原数组
            int[] memory = new int[100];        //备忘录
            int[] maxnub = new int[numbers.length];    //存储最大值
            int[] end = new int[numbers.length];//子序列最后的位置
            for (int i = 0; i < numbers.length; i++) {
                maxnub[i] = numbers[i];
                memory[i] = numbers[i];
                for (int j = i+1; j < numbers.length; j++) {
                    memory[j] = memory[j-1]+numbers[j];
                    if (memory[j] > maxnub[i]) {
                        maxnub[i] = memory[j];
                        end[i] = j;
                    }
                }
                for (int k = 0; k <= numbers.length; k++) {
                    memory[k] = 0;
                }
            }
            System.out.println("每个数字对应最大值情况");
            for (int i = 0; i < numbers.length; i++) {
                System.out.print(maxnub[i]+"\t");
            }
            System.out.println("\n");
            int max = maxnub[0];
            w = end[0];
            for (int i = 1; i < maxnub.length; i++) {        
                if (maxnub[i] > max) {
                    max = maxnub[i];
                    s = i;
                    w = end[i];
                }
            }
            System.out.println("最大连续子序列和为:"+max);
            System.out.println("子序列内容如下:");
            for (int i = s; i <= w; i++) {
                System.out.print(numbers[i]+",");
            }
    
        }
    
    }
    

    image.png


    我的代码能力没有训练过,就这一道题花费4小时左右,可见愚钝,但是代码都是我一个一个敲出来的,没有参考谁,难得有此机会,说一说自己的想法。大家应该关心核心代码,对于前面变量和数组的定义不要想为什么你会想到一开始就写这么多,实际上不是这样的,只是写基本要用的,后面需要的再去添加。其次就是Debug的使用,这是非常关键的,今天也是在这里停留一段时间,决定记录一下eclipse的这个功能。再者,我会利用此次代码融会贯通前面的动态规划的思想


    Debug功能学习
    单击右键
    image.png
    鼠标指向最右侧(蓝色部分),鼠标左键双击实现断点
    断点的含义是,执行断点前的代码直到断点处结束
    image.png
    然后,再次执行
    image.png
    最后
    image.png
    点击变量选项
    image.png


    关注 image.png
    Resume - Suspend - Terminate - Disconnect - Step Into - Step Over - Step Return
    image.png


    删除Debug测试
    点击Run—>Debug Configurations
    image.png


    融会贯通动态规划思想