zoukankan      html  css  js  c++  java
  • 课堂随笔

    package project;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class read1
    {
        class KadaneResult
        {
            int maxSum;
            int start;
            int end;
    
            public KadaneResult(int maxSum, int start, int end)
            {
                this.maxSum = maxSum;
                this.start = start;
                this.end = end;
            }
        }
    
        public ArrayList<String> kadane(int arr[])
        {
            ArrayList<String> process = new ArrayList<>();
            int max = 0;
            int maxStart = -1;
            int maxEnd = -1;
            int currentStart = 0;
            int maxSoFar = 0;
            for (int i = 0; i < arr.length; i++)
            {
                maxSoFar += arr[i];
                if (maxSoFar < 0)
                {
                    maxSoFar = 0;
                    currentStart = i + 1;
                }
                if (max < maxSoFar)
                {
                    maxStart = currentStart;
                    maxEnd = i;
                    max = maxSoFar;
                }
                process.add(i + ":" + printOut(arr, maxStart, maxEnd, maxSoFar));
            }
            return process;
        }
    
        private String printOut(int arr[], int maxStart, int maxEnd, int maxSoFar)
        {
            String aString = new String();
            for (int i = 0; i < arr.length; i++)
            {
    
                if (i == maxStart || i == maxEnd)
                {
                    aString += ("[" + arr[i] + "]");
                } else
                {
                    aString += (arr[i]);
                }
                if (i < arr.length - 1)
                {
                    aString += (" ");
                } else
                {
                    aString += (" 
    当前最大子数组和为{" + maxSoFar + "}");
                }
            }
            return aString;
        }
    
        public static void main(String[] args)
        {
            read1 arrayList = new read1();
            int[] a = { 1, 3, -9, 6, 2, 4, 1, 5, 8 };
            ArrayList<String> arrayList2 = arrayList.kadane(a);
            Scanner inScanner = new Scanner(System.in);
            int i = -1;
            boolean flag = true;
            while (flag)
            {
                System.out.println("1.下一步 2.上一步 3.回滚到第几步");
                switch (inScanner.nextInt())
                {
                case -1:
                    System.out.println("已经到顶");
                case 1:
                    if (i + 1 >= arrayList2.size())
                    {
                        System.out.println("已经到底");
                        break;
                    }
                    System.out.println(arrayList2.get(++i));
                    break;
                case 2:
                    System.out.println(arrayList2.get(--i));
                    break;
                case 3:
                    System.out.println("回滚到第..步");
                    i = inScanner.nextInt();
                    System.out.println(arrayList2.get(i));
                    break;
                default:
                    flag = false;
                    break;
                }
    
            }
        }
    }

  • 相关阅读:
    [干货向]用Javascript获取页面元素的位置
    从 JavaScript 数组去重谈性能优化
    《悟透javascript》中的知识点
    深入理解javascript闭包
    深入理解Javascript之执行上下文(Execution Context)
    CSS Sprites的原理
    深入理解JavaScript定时机制
    防范sql注入式攻击的比较有见地的代码(PHP)
    PHP(authcode)加密解密
    预防数据库攻击
  • 原文地址:https://www.cnblogs.com/Excusezuo/p/10770767.html
Copyright © 2011-2022 走看看