zoukankan      html  css  js  c++  java
  • FindLongArray

    package findlongarray;

    import java.util.Stack;

    class LongArray {

    int array[];
    Stack stack;

    public LongArray() {
    }

    public LongArray(int i) {
    array = new int[10];
    {
    for (int j = 0; j < i; j++) {
    array[j] = (int) (Math.random() * 26);
    System.out.print(array[j] + " ");
    }
    }
    }

    public int findMaxarry() //判断当前数组最大递增个数,
    {
    System.out.println();
    Object arraymax[]=new Object[10];
    for(int i=0;i<arraymax.length;i++){arraymax[i]=-1;}
    // int array[]={16,3,4,24,13,18,15,4,19,6};
    for(int i=0;i<array.length;i++)
    {
    System.out.print(array[i] + " ");
    }
    Stack stacktemp;
    Stack stackarray = new Stack();
    int itemp = 0;//栈的长度
    int jtemp = 0;//下标。
    int maxtemp = 0;
    for (int i = 0; i < array.length; i++) {
    stacktemp = new Stack();
    stacktemp.push(i);
    for (int j = i + 1; j < array.length; j++) {
    jtemp = (int) stacktemp.peek();
    if (array[j] > array[jtemp]) {
    stacktemp.push(j);
    }
    }//构造了一个以栈,存放当前包含碰到的直接递增的数组,进出栈,不断试探。
    itemp = stacktemp.size();//取数组长度

    if (itemp > maxtemp)//当前构造的数组长于之前,替换之。
    {

    maxtemp = itemp;
    System.out.println("当前 " + stackarray.toString() );
    arraymax=stacktemp.toArray();
    stackarray = stacktemp;//
    System.out.println("更后 " + stackarray.toString()+"长度为" + maxtemp);
    }
    // System.out.println(stacktemp.toString());
    int ktemp = i + 1;
    while (stacktemp.size() > 1 && ktemp != array.length - 1) {
    ktemp = (int) stacktemp.pop() + 1;
    for (int k = ktemp; k < array.length; k++) {
    jtemp = (int) stacktemp.peek();
    if (array[k] > array[jtemp]) {
    stacktemp.push(k);
    System.out.println( "插入之后"+stacktemp.toString());
    }
    }
    System.out.println("跳出");
    itemp = stacktemp.size();//取数组长度
    if (itemp > maxtemp)//当前构造的数组长于之前,替换之。
    { maxtemp = itemp;
    stackarray = stacktemp;
    arraymax=stacktemp.toArray();
    }
    }

    }
    System.out.println("\n"+arraymax.toString());
    for(int i=0;i<arraymax.length;i++)
    {
    System.out.print(arraymax[i]+" ");
    }

    return maxtemp;
    }
    }

    /**
    *
    * @author Administrator
    */
    public class FindLongArray {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    LongArray la=new LongArray(10);
    la.findMaxarry();
    }
    }

  • 相关阅读:
    Windows Server 2003 SP2(32位) 中文版 下载地址 光盘整合方法
    用Recycle()方法对Java对象的重要性
    Lotus中千奇百怪的 $$
    Developing a simple application using steps "User Decision" and "Mail"(1) 沧海
    沟通中的情绪管理(演讲稿) 沧海
    人只有在压力之下,才可能成功,没做一件事,都必须成功,不许言败 沧海
    什么是IDOC,以及IDOC的步骤 沧海
    VS2008 Professional Edition CHS中的deffactory.dat读取错误 沧海
    Including custom text in the step "User Decision" 沧海
    SAP Upgrade Strategy 沧海
  • 原文地址:https://www.cnblogs.com/xiekai/p/3491578.html
Copyright © 2011-2022 走看看