码迷,mamicode.com
首页 > 编程语言 > 详细

斐波那契非递归算法

时间:2016-08-16 10:28:54      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

    斐波那契数列求第n项的非递归算法实现

 1 #include<iostream>
 2 
 3 long long Fibonacci(unsigned int n){
 4    if(n<=2)
 5     return n-1;
 6 
 7    long long fibNumberOne = 1;
 8    long long fibNumberTwo = 0;
 9    long long fibN = 0;
10    for(unsigned int i = 2;i < n;i++){
11         fibN = fibNumberOne + fibNumberTwo;
12         fibNumberTwo = fibNumberOne;
13         fibNumberOne = fibN;
14    }
15    return fibN;
16 }
17 
18 int main(){
19     unsigned int fiboNumber;
20     while(std::cin>>fiboNumber){
21         std::cout<<Fibonacci(fiboNumber)<<std::endl;
22     }
23     return 0;
24 }

 

斐波那契非递归算法

标签:

原文地址:http://www.cnblogs.com/yangrenzhi/p/5775342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!