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

之前的写法比较屌丝,从同事那来个比较高大上的,让脑袋转转

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=0
n--;
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值