剑指offer题七
来源:互联网 发布:淘宝免费充话费 编辑:程序博客网 时间:2024/06/07 19:09
//大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(Fibonacci(n));
}
public static int Fibonacci(int n) {
//循环处理
if(n<=1){
return n;
}
int[] result = new int[n+1];
result[0] = 0;
result[1] = 1;
for(int i = 2;i<=n && i<=39;i++){
result[i] = result[i-1]+result[i-2];
}
return result[n];
//递归处理
if(n<=1){
return n;
}else{
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(Fibonacci(n));
}
public static int Fibonacci(int n) {
//循环处理
if(n<=1){
return n;
}
int[] result = new int[n+1];
result[0] = 0;
result[1] = 1;
for(int i = 2;i<=n && i<=39;i++){
result[i] = result[i-1]+result[i-2];
}
return result[n];
//递归处理
if(n<=1){
return n;
}else{
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
0 0
- 剑指offer题七
- 剑指offer题七
- 剑指offer 七
- 剑指offer之编程(七)
- 剑指offer经典编程(七)
- 剑指offer之七---跳台阶
- 《剑指offer》面试题七 用两个栈实现队列
- 剑指offer面七_用两个队列实现栈
- 剑指offer系列之七:斐波那契数列
- 剑指offer系列之四十七:不用加减乘除做加法
- 剑指offer(七)之二进制中1的个数
- 剑指Offer----面试题七:用两个栈实现队列
- 剑指offer(四十七)之平衡二叉树
- (七)剑指offer之Fibonacci数列及其变形
- 剑指Offer题目JAVA版思路与代码(七)
- 剑指offer(七)斐波那契数列
- 剑指offer面试题七之重建二叉树
- 剑指offer第一题
- Velocity代码片段
- 札记2
- 浅谈Android源码与设计模式
- @JSONField
- ubuntu16.04空间扩容
- 剑指offer题七
- 设置ODBC数据源时候更改默认数据库时没有新建的数据库
- Leetcode :palindrome partitioning(c++)
- Spring概念
- 剑指offer题六
- 蓝桥杯 分巧克力(二分)
- MySQL基本操作
- 闭包与匿名函数
- Colliders CodeForces