tonglin0325的个人主页

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

#

 1.递归法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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.非递归法#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
}

}