本文共 624 字,大约阅读时间需要 2 分钟。
斐波那契数列的计算通常是通过递归解决的,不过如果将递归转化为迭代,可以有效提高算法效率,代码如下:
//fibonacci_iterationint fib(int n){ int f=1,g=0; while(0
注意,此时算法的复杂度为O(n) ,优于二分递归的O(2^n) .
这里我采用的是vs2015搭配MASM32,以及Kip Irvine的链接库,代码如下:
include Irvine32.inc.dataemp BYTE " ",0.codemain PROC mov eax,0;g mov ebx,1;f mov ecx,25 ;输出前25项 L: add eax,ebx;g+=f mov edx,eax sub edx,ebx mov ebx,edx;f=g-f call WriteDec ;以32位无符号数的形式输出eax mov edx,OFFSET emp call WriteString ;输出空格(即edx对应的字符串) loop L exit main ENDP END main
其中的WriteDec以及WriteString都是Irvine32库内的函数,功能如注释所示
copyright:swy
转载地址:http://bzwai.baihongyu.com/