剑指offer——斐波那契数列
来源:互联网 发布:js有哪些内置对象 编辑:程序博客网 时间:2024/05/21 10:19
思路:斐波那契数列属于非常经典的递归问题,但也可以用循环做。
/** * @author Tom Qian * @email tomqianmaple@outlook.com * @github https://github.com/bluemapleman * @date 2017年8月10日 *///大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。// n<=39public class 斐波那契数列{ //递归 public static int Fibonacci(int n){ if(n<=0) return 0; if(n==1 || n==2) return 1; return Fibonacci(n-1)+Fibonacci(n-2); } //循环 public static int iteFibonacci(int n){ if(n<1){ return 0; } if(n<3){ return 1; } int count=0; int count1=1; int count2=1; for(int i=3;i<=n;i++){ count=count1+count2; count1=count2; count2=count; } return count; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(iteFibonacci(4)); }}
阅读全文
0 0
- 《剑指offer》——斐波那契数列
- 剑指offer——斐波那契数列
- 斐波那契数列——剑指offer
- 剑指offer——斐波那契数列
- 剑指offer——斐波那契数列
- 剑指offer——7.斐波那契数列
- 剑指offer——斐波那契数列
- 剑指offer——斐波那契数列
- 剑指offer—斐波那契数列
- 剑指offer刷题—斐波那契数列
- 《剑指offer》—7、斐波那契数列
- 剑指offer(7)—斐波那契数列
- 【剑指offer】斐波那契数列
- 剑指offer--斐波那契数列
- 剑指Offer-斐波那契数列
- 剑指offer:斐波那契数列
- 《剑指offer》斐波那契数列
- 剑指 offer:斐波那契数列
- uboot 常用指令集
- automake编译工程
- pdf转word,免费网站
- grafana在login界面白屏(blank screen)及账号密码无法更改
- Codeforces Round #429 (Div 2) A
- 剑指offer——斐波那契数列
- 回文 (dp)
- es6中为什么要bind(this)
- Discuz触屏手机版显示分类信息的修改方法
- 【leetcode】第48题 Rotate Image 题目+解析+代码
- iOS_Runtime4_动态添加属性
- 基于AspectJ的注解方式进行AOP开发
- Axure chrome 扩展显示已损坏的解决方法 写在前面的话: 之前用的好好的Axure chrome 扩展如今却并不能使用,一直说扩展已损坏,额,上网下了几个.crx文件都没有用……
- pygame按键对应