斐波那契堆栈实现(递归的代码实现)

来源:互联网 发布:河南网络信访平台 编辑:程序博客网 时间: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



0 0