太久没写代码了,今天看视频教程,谈到用非递归方法实现斐波那契数列,居然愣了一下,煞笔了……
#include<stdio.h>
int fab(int a) {
if(a<=2)return 1;
int pre = 0;
int cur = 1;
for(int i=2;i<=a;i++) {
int temp = pre;
pre = cur ;
cur = temp + pre;
}
return cur;
}
int main()
{
int a;
//1 1 2 3 5 8 13
while(scanf("%d",&a)!=EOF){
printf("%d\n",fab(a));
}
return 0;
}