F1: 迭代法 最慢,复杂度最高 F2: 直接法 F3: 矩阵法 参考<算法之道(The Way of Algorithm)>第38页-魔鬼序列:斐波那契序列 F4: 通项公式法 由于公式中包含根号5,无法取得精确的结果,数字越大误差越大 using System; using System.Diagnostics; namespace Fibonacci { class Program { static void Main(string[] args) { ulong result; ; C
1: # 计算Fibonacci数: # Naive版本,时间效率O(1.618^n) # 记忆化版本(增加line8.10.13),时间效率O(n) # 注意:当n超过1000,可能超过系统允许的最大递归深度 from time import process_time # memo = {} def fib(n): # if n in memo: return memo[n] if n < 2: f = n else: f = fib(n-2) + fib(n-1) # memo[n] = f
本章问题 1.具有空函数体的函数可以作为存根使用,你如何对这类函数进行修改,使其更有用? answer:Have the stub(存根) print out a message when it is called,perhaps printing the values it was given as arguments. (存根可以在它被调用的时候打印出相应的信息,可能打印出参数的值) 2.在ANSI C中,函数的原型非必需,请问这个规定是优点还是缺点? answer:An advantage