利用堆栈输出递归序列的若干项:Fibonacci序列
2009-11-21 16:54
351 查看
package com.fuxi.test.collection;
/**
* 利用堆栈输出递归序列的若干项:Fibonacci序列
* 堆栈是一种“先进后出”的特殊序列
*/
import java.util.Stack;
public class StackTest {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(new Integer(1));
stack.push(new Integer(1));
int k =1;
while(k<10){
Integer F1 = stack.pop();
int f1 = F1.intValue();
Integer F2 = stack.pop();
int f2 = F2.intValue();
Integer temp = new Integer(f2+f1);
System.out.println(temp.toString());
stack.push(temp);
stack.push(F2);
k++;
}
}
}
/**
* 利用堆栈输出递归序列的若干项:Fibonacci序列
* 堆栈是一种“先进后出”的特殊序列
*/
import java.util.Stack;
public class StackTest {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(new Integer(1));
stack.push(new Integer(1));
int k =1;
while(k<10){
Integer F1 = stack.pop();
int f1 = F1.intValue();
Integer F2 = stack.pop();
int f2 = F2.intValue();
Integer temp = new Integer(f2+f1);
System.out.println(temp.toString());
stack.push(temp);
stack.push(F2);
k++;
}
}
}
相关文章推荐
- 利用递归求Fibonacci序列
- Fibonacci(斐波那契)序列的递归和非递归算法
- Magical GCD UVA 1642 利用约数个数少来优化 给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量的值最大。输出这个最大值。
- POj2255 利用先序,中序输出后序序列
- Fibonacci(斐波那契)序列的递归和非递归
- 深度学习-利用递归网络判断序列趋势
- 建立有向图的邻接表,深度优先遍历和广度优先遍历的递归与非递归算法,判断是否是有向无环图,并输出一种拓扑序列
- Fibonacci序列(递归-傻孩子版)
- 利用递归求最大子序列
- Fibonacci(斐波那契)序列的递归和非递归算法
- 第十二周项目 3 用递归方法求解(输出Fibnacci序列的第20个数)
- 第4周项目5:用递归方法输出Fibnacci序列的第20个数
- 利用递归实现对输入字符串的倒序输出
- 利用函数递归逆序输出一个字符串
- 【数据结构】-线性表-链表 熟练度max=6(利用递归倒序输出L中的值域)
- java利用dom4j递归输出所有节点
- 有一个队列 ABCDEFG, 现有一个堆栈, 有三种操作, 出队, 入栈,出栈. 输出可以是出队操作也可以是出栈操作.试问如何判断某一序列不是无法输出的?
- USTC机试-利用递归实现组合数的输出
- 利用先序和中序非递归恢复二叉树,并后序遍历输出
- n个元素进栈,输出所有出栈序列-卡特兰数-递归