Java递归算法——三角数字(消除递归)

 

import java.io.*;                 // for I/O


//类名:Params
//属性:
//方法:
class Params     //这个类的对象被压入栈中
   {
   public int n;							//用来存放键盘输入的数字
   public int returnAddress;	//返回的地址

   public Params(int nn, int ra)
      {
      n=nn;
      returnAddress=ra;
      }
   }  // end class Params


//类名:StackX
//属性:
//方法:
class StackX
   {
   private int maxSize;         // size of StackX array
   private Params[] stackArray;
   private int top;             // top of stack
//--------------------------------------------------------------
   public StackX(int s)         // constructor
      {
      maxSize = s;              // set array size
      stackArray = new Params[maxSize];  // create array
      top = -1;                 // no items yet
      }
//--------------------------------------------------------------
   public void push(Params p)   // put item on top of stack
      {
      stackArray[++top] = p;    // increment top, insert item
      }
//--------------------------------------------------------------
   public Params pop()          // take item from top of stack
      {
      return stackArray[top--]; // access item, decrement top
      }
//--------------------------------------------------------------
   public Params peek()         // peek at top of stack
      {
      return stackArray[top];
      }
//--------------------------------------------------------------
   }  // end class StackX


//类名:stackTriangle
//属性:
//方法:
class stackTriangle
   {
   static int theNumber;		//用于接收输入的int
   static int theAnswer;
   static StackX theStack;
   static int codePart;				//用于switch选择
   static Params theseParams;

   public static void main(String[] args) throws IOException
      {
      System.out.print("Enter a number: ");
      theNumber = getInt();		//接收键盘输入的int
      recTriangle();
      System.out.println("Triangle="+theAnswer);
      }  // end main()

   public static void recTriangle()
      {
      theStack = new StackX(10000);
      codePart = 1;
      while( step() == false)  // call step() until it's true
         ;                     // null statement
      }
//-------------------------------------------------------------
   public static boolean step()
      {
      switch(codePart)
         {
         case 1:                              // initial call
        	 System.out.println("进入1");
            theseParams = new Params(theNumber, 6);	
            theStack.push(theseParams);
            codePart = 2;
            break;
            
         case 2:                              // method entry
        	 System.out.println("进入2");
            theseParams = theStack.peek();	//对输入的数字一直减1,直到等于1,如果大于1就跳到3中,压入栈中
            if(theseParams.n == 1)            		// n是键盘输入的数字,如果是1,结果是1,codePart跳到5
               {
               theAnswer = 1;
               codePart = 5;   // exit
               }
            else														//如果大于1,就跳到3
               codePart = 3;   // recursive call
            break;
            
         case 3:                              
        	 System.out.println("进入3");
            Params newParams = new Params(theseParams.n - 1, 4);
            theStack.push(newParams);			//把输入的数字减去1,并压入栈中
            codePart = 2;  										//回到2中判断数组减去1后,是否等于1
            break;
            
         case 4:                              // calculation
        	 System.out.println("进入4");
            theseParams = theStack.peek();			//取得2
            theAnswer = theAnswer + theseParams.n;	//1+2
            codePart = 5;
            break;
            
         case 5:                              // method exit
        	 System.out.println("进入5");
            theseParams = theStack.peek();
            codePart = theseParams.returnAddress; //在2和3中交替跳转后,结束时跳到5,此时栈中codePart除了栈底是6,其他都是4
            theStack.pop();				//在取得了下次跳转的位置后,出栈,第一次出栈的是(1,4)
            break;
            
         case 6:                              // return point
        	 System.out.println("进入6");
            return true;
         }  // end switch
      return false;
      }  // end triangle
//-------------------------------------------------------------
   public static String getString() throws IOException
      {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      String s = br.readLine();
      return s;
      }
//-------------------------------------------------------------
   public static int getInt() throws IOException
      {
      String s = getString();
      return Integer.parseInt(s);
      }

   }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值