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

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

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
    评论
### 回答1: 以下是使用C语言堆栈实现Fibonacci数列归求值的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 int stack[MAX_SIZE]; int top = -1; void push(int data) { if (top == MAX_SIZE - 1) { printf("Stack overflow\n"); exit(1); } top++; stack[top] = data; } int pop() { if (top == -1) { printf("Stack underflow\n"); exit(1); } int data = stack[top]; top--; return data; } int fibonacci(int n) { if (n <= 1) { return n; } else { push(n); push(n - 1); int a = fibonacci(pop()); int b = fibonacci(pop()); return a + b; } } int main() { int n = 6; int result = fibonacci(n); printf("Fibonacci(%d) = %d\n", n, result); return 0; } ``` 在这个例子中,我们使用了一个数组来模拟堆栈。`push`函数用于将数据压入堆栈中,`pop`函数用于从堆栈中弹出数据。`fibonacci`函数使用归方式计算Fibonacci数列的值。当归到一个较小的问题时,我们将其参数压入堆栈中,然后通过弹出堆栈中的参数来计算更大的问题。最终结果被返回给调用者。 在这个例子中,我们计算Fibonacci数列的第6项,即Fibonacci(6)。程序输出如下: ``` Fibonacci(6) = 8 ``` 因此,Fibonacci数列的第6项的值为8。 ### 回答2: 在C语言中,可以使用堆栈(stack)来实现Fibonacci数列的归求值。 首先,我们需要定义一个结构体类型来表示堆栈的节点,包含一个整数变量用于存储Fibonacci数列的值,以及一个指针变量用于指向下一个节点。 ```c typedef struct Node { int value; struct Node* next; } Node; ``` 接下来,我们需要定义一个函数来将节点入栈,即在堆栈的顶部插入一个新节点。该函数接受一个整数参数,表示要入栈的Fibonacci数列值。 ```c void push(Node** stack, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->value = value; newNode->next = *stack; *stack = newNode; } ``` 然后,我们需要定义一个函数来从堆栈中取出节点,即将堆栈的顶部节点删除并返回其值。该函数不接受任何参数,返回一个整数值表示取出的Fibonacci数列值。 ```c int pop(Node** stack) { if (*stack == NULL) { return -1; // 堆栈为空 } Node* topNode = *stack; int topValue = topNode->value; *stack = (*stack)->next; free(topNode); return topValue; } ``` 最后,我们可以定义一个归函数来计算Fibonacci数列的值。该函数接受一个整数参数n,表示要计算的Fibonacci数列的第n个数。基于归的定义,我们可以将计算过程拆分为两个子问题:计算第n-1个数和第n-2个数的值。然后,我们将这两个子问题的解压入堆栈中,并返回它们的和(即第n个数的值)。 ```c int fibonacci(int n) { if (n <= 1) { return n; // n为0或1时,返回n本身 } Node* stack = NULL; push(&stack, n-1); push(&stack, n-2); int result = fibonacci(pop(&stack)) + fibonacci(pop(&stack)); return result; } ``` 在主函数中,我们可以调用上述归函数来计算Fibonacci数列的值,并输出结果。 ```c int main() { int n = 10; int result = fibonacci(n); printf("The %dth Fibonacci number is: %d\n", n, result); return 0; } ``` 通过使用堆栈实现Fibonacci数列的归求值,可以有效避免归调用过深造成的栈溢出问题,并且具有较好的时间复杂度和空间复杂度性能。 ### 回答3: C语言实现Fibonacci数列的归求值需要使用堆栈来保存归函数调用时的局部变量和返回地址。堆栈是一种数据结构,它采用"先进后出"的原则,类似于一堆盘子堆在一起,后放进去的盘子要先取出来。 在C语言中,可以使用数组来模拟堆栈。首先,定义一个数组作为堆栈,同时定义一个变量top作为堆栈的指针,指向堆栈顶部元素的下一个位置。然后,将初始值设为-1表示堆栈为空。 接下来,定义一个归函数来计算Fibonacci数列的值。该函数接受一个参数n,表示要计算的Fibonacci数列的第n个元素。在函数内部,首先检查n是否小于等于1,如果是,则直接返回n的值作为Fibonacci数列的第n个元素。如果n大于1,则将n的值入栈,然后将n的值减一作为参数归调用该函数,并将返回值存储在一个临时变量中。然后,将n的值再减一入栈,继续归调用函数,同样将返回值存储在临时变量中。最后,将两个临时变量相加得到Fibonacci数列的第n个元素的值,并将结果返回。 在每次函数调用返回后,需要将堆栈中保存的值出栈,恢复函数调用前的状态。具体操作如下:在函数开始时将n的值入栈,函数返回前将n的值出栈,并将返回值存入临时变量中。每次归调用结束后,将归函数的返回值存入一个临时变量,并将保存n值的堆栈位置top减一,以便出栈n的值。 最后,将实现以上步骤的代码编译运行,调用归函数并传入要计算的Fibonacci数列的元素位置作为参数,即可得到所需的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值