利用动态规则的思路,摒弃传统的递归做法,可以得到一种快速的求fibonacci第n个数的算法: ''' 求第n(从1开始)位fibonacci数 fibonacci数列前两位为0, 1. 后面每一位数字等于前两位数字之和 ''' def fibonacci( n ): if n <= 2: return n - 1 f = 0 g = 1 while n - 2 > 0: g = g + f f = g - f n -= 1 return g print( fibonacci( 100 ) )…
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as po…
程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; procedure panduan; begin i:=2; while i<m do begin n1:=n1+n2; n2:=n1+n2; i:=i+1; write(n2); end; end; begin read(m); n1:=1; n2:=1; if m=2 then write(n1,n2…
接触ACM没几天,向各路大神求教,听说ACM主要是研究算法,所以便开始了苦逼的算法学习之路.话不多说,RT所示,学习快速求幂. 在头文件<math.h>或是<cmath>中,double pow( double x, double y );函数是用来快速求x^y,于是便从pow函数来说起,以下大体上是pow的函数代码: int pow(int x, int n) { int num = 1; while (n != 0){ num = num *x; n = n -1; } ret…