Java递归算法——三角数字(递归和非递归)

1.递归法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class triangle_demo {
//static int theNumber;
public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
System.out.println("输入数字:");
int theNumber = getInt();
int theAnswer = triangle(theNumber);
System.out.println("三角上每一行的数量:"+theAnswer);
}
public static int triangle(int n){ //递归输出1 3 6 10....
if(n==1)
return 1;
else
return (n + triangle(n-1));
}
//输出方法
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);
}
}




2.非递归法
import java.io.*; // for I/O
class Params //这个类的对象被压入栈中
{
public int n; //用来存放键盘输入的数字
public int returnAddress; //返回的地址
public Params(int nn, int ra)
{
n=nn;
returnAddress=ra;
}
} // end class Params
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
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);
}
}
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5357501.html

浙公网安备 33010602011771号