微软100题-天天做-第19题
来源:互联网 发布:java多线程并发问题 编辑:程序博客网 时间:2024/05/14 15:33
第19题(数组、递归):
题目:定义Fibonacci数列如下:
/ 0 n=0
f(n)= 1 n=1
/ f(n-1)+f(n-2) n=2
输入n,用最快的方法求该数列的第n项。
分析:在很多C语言教科书中讲到递归函数的时候,都会用Fibonacci作为例子。
因此很多程序员对这道题的递归解法非常熟悉,但....呵呵,你知道的。
package com.microsoft;public class Fibonacci {private int n;private int[]memory;public Fibonacci(int n){this.n=n;memory=new int[n+1];for(int i=0;i<n+1;i++){memory[i]=-1;}}public int calculate(){return calculate(n);}public int calculate(int n){if(n==1){return 1;}if(n==0){return 0;}if(memory[n]!=-1){return memory[n];}int ret=calculate(n-1)+calculate(n-2);memory[n]=ret;return ret;}public static void main(String[] args) {Fibonacci f=new Fibonacci(20);System.out.println(f.calculate());}}
0 0
- 微软100题-天天做-第19题
- 微软面试100题-天天做-第5题
- 微软面试100题-天天做-第6题
- 微软100题-天天做-第7题
- 微软100题-天天做-第8题
- 微软100题-天天做-第9题
- 微软100题-天天做-第10题
- 微软100题-天天做-第11题
- 微软100题-天天做-第12题
- 微软100题-天天做-第13题
- 微软100题-天天做-第14题
- 微软100题-天天做-第15题
- 微软100题-天天做-第16题
- 微软100题-天天做-第17题
- 微软100题-天天做-第18题
- 微软100题-天天做-第20题
- 微软100题-天天做-第21题
- 微软100题-天天做-第22题
- 2013阿里巴巴实习生笔试
- 【类加载器】————类加载器之间的父子关系以及管辖范围【委托机制】
- C#virtual override new
- 伪随机数
- Eclipse和Pydev鼓捣笔记
- 微软100题-天天做-第19题
- Visual Studio 2013
- 远程桌面协议(RDP)工具 FreeRDP
- linux errno定义
- C#对HTML文档的解析
- 随机数与伪随机数
- 08 在ubuntu中截图的方法
- 伪随机数
- MersenneTwister算法