标签:interview
8.1 Write a method to generate the nth Fibonacci number.
class Fibonacci() { void init() { a = 0; b = 1; } int next() { int toReturn = a + b; a = b; b = toReturn; return toReturn; } } { Fibonacci fibo = init(); for (n) { toReturn = fibo.next(); } return toReturn; } fibonacci(int n) { if (n == 0) return 1; else if (n == 1) return 2; else return fibonacci(n - 1) + fibonacci(n - 2); }
标签:interview
原文地址:http://7371901.blog.51cto.com/7361901/1583712