斐波那契堆栈实现(递归的代码实现)
来源:互联网 发布:河南网络信访平台 编辑:程序博客网 时间:2024/05/17 08:19
之前的写法比较屌丝,从同事那来个比较高大上的,让脑袋转转
package ext.pack.test;import java.util.Stack;public class HelloWorld extends TestInterface {public static <T> void printStack(Stack<T[]> stack) {System.out.println("STACK:BEGIN");int STACK_SIZE = stack.size();for (int i = STACK_SIZE; i > 0; i--) {T[] objs = stack.get(i - 1);for (T obj : objs) {System.out.print(obj + ",");}System.out.println();}System.out.println("STACK:END");}public static void c2(int n) {long s = 0, rt = 0;int branch = 0;Stack<Object[]> stack = new Stack<Object[]>();while (true) {if (n < 2) {// this will goto last one, as nothing to operate// pop next things to operate, define $rt as last result in {0,1}// we have the result, there is no need for current stack.rt = n;System.out.println("Print $$n<2, n="+n);} else {switch (branch) {case 0: {do {stack.push(new Object[] { branch, 0l, n });n -= 2;} while (n >= 2);System.out.print("branch=0,");printStack(stack);System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");continue;}case 1: {// push the F(n-1)stack.push(new Object[] { branch, rt, n });// process F(n-2), goto $BRANCH=0n--;branch = 0;System.out.print("branch=1,");printStack(stack);System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");continue;}case 2: {// calc rs til now in one F(n),calc from $BRANCH=1, $BRANCH++// we have the result, there is no need for current stack.System.out.println("We have the previous Result!!");rt += s;break;}default:break;}}if (stack.isEmpty()) {System.out.println("Result is :" + rt);return;}Object[] t = stack.pop();branch = (Integer) t[0];s = (Long) t[1];n = (Integer) t[2];System.out.println("POPUP Branch=" + branch + " s=" + s + " n=" + n + " rt=" + rt);printStack(stack);System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");branch++;}}public static void main(String[] args) {c2(6);}}
Log分析:
branch=0,STACK:BEGIN
0,0,2,
0,0,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=0
POPUP Branch=0 s=0 n=2 rt=0
STACK:BEGIN
0,0,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,0,2,
0,0,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=1 s=0 n=2 rt=1
STACK:BEGIN
0,0,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=0 s=0 n=4 rt=1
STACK:BEGIN
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=0,STACK:BEGIN
0,0,3,
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=0 s=0 n=3 rt=1
STACK:BEGIN
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,1,3,
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=0,STACK:BEGIN
0,0,2,
1,1,3,
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=0
POPUP Branch=0 s=0 n=2 rt=0
STACK:BEGIN
1,1,3,
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,0,2,
1,1,3,
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=1 s=0 n=2 rt=1
STACK:BEGIN
1,1,3,
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=1 s=1 n=3 rt=1
STACK:BEGIN
1,1,4,
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=1 s=1 n=4 rt=2
STACK:BEGIN
0,0,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=0 s=0 n=6 rt=3
STACK:BEGIN
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=0,STACK:BEGIN
0,0,3,
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=0 s=0 n=3 rt=1
STACK:BEGIN
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,1,3,
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=0,STACK:BEGIN
0,0,2,
1,1,3,
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=0
POPUP Branch=0 s=0 n=2 rt=0
STACK:BEGIN
1,1,3,
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,0,2,
1,1,3,
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=1 s=0 n=2 rt=1
STACK:BEGIN
1,1,3,
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=1 s=1 n=3 rt=1
STACK:BEGIN
0,0,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=0 s=0 n=5 rt=2
STACK:BEGIN
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=0,STACK:BEGIN
0,0,2,
0,0,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=0
POPUP Branch=0 s=0 n=2 rt=0
STACK:BEGIN
0,0,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,0,2,
0,0,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=1 s=0 n=2 rt=1
STACK:BEGIN
0,0,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=0 s=0 n=4 rt=1
STACK:BEGIN
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=0,STACK:BEGIN
0,0,3,
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=0 s=0 n=3 rt=1
STACK:BEGIN
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,1,3,
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=0,STACK:BEGIN
0,0,2,
1,1,3,
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=0
POPUP Branch=0 s=0 n=2 rt=0
STACK:BEGIN
1,1,3,
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
branch=1,STACK:BEGIN
1,0,2,
1,1,3,
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Print $$n<2, n=1
POPUP Branch=1 s=0 n=2 rt=1
STACK:BEGIN
1,1,3,
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=1 s=1 n=3 rt=1
STACK:BEGIN
1,1,4,
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=1 s=1 n=4 rt=2
STACK:BEGIN
1,2,5,
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=1 s=2 n=5 rt=3
STACK:BEGIN
1,3,6,
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
POPUP Branch=1 s=3 n=6 rt=5
STACK:BEGIN
STACK:END
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
We have the previous Result!!
Result is :8
- 斐波那契堆栈实现(递归的代码实现)
- Fibonacci(斐波那契)数列的递归与非递归实现 python
- 斐波那契数列-递归实现
- 斐波那契数列(递归实现)
- 斐波那契数列-递归实现
- 斐波那契数列的非递归实现
- 斐波那契数列的递归优化实现
- 斐波那契数列的递归实现
- 斐波那契数列的非递归实现 JAVA
- 斐波那契数列的非递归实现
- 斐波那契数列的递归实现
- 斐波那契数列的非递归实现
- python递归式的实现斐波那契数列
- fibonacii数列(斐波那契数列)的递归实现及循环实现
- 斐波那契数列的递归实现和非递归实现
- 斐波那契数列的递归实现和非递归实现
- 实现斐波那契数列的递归、非递归及尾递归。
- 挑战高楼(斐波那契非递归实现)
- 划分ospf区域方式
- 两个常用的UIControl前缀
- ARM Linux 3.x的设备树(Device Tree)
- JS刷新当前页面
- PostgreSQL 存储过程
- 斐波那契堆栈实现(递归的代码实现)
- lua面向对象编程之点号(.)与冒号(:)的差异详细比较
- A FIFO (First In First Out)
- Cocos2d-x入门之常用类介绍
- Redis 命令参考
- 通过LeakDiag查看内存泄漏
- 更改aspx页面编码格式
- Uni2D 入门 -- Atlas
- oc学习之旅:协议