[ create a new paste ] login | about

Link: http://codepad.org/UINMDR8A    [ raw code | output | fork ]

C++, pasted on Apr 9:
#include<iostream>
using namespace std;
long diedai(int n)
{
long result;
long p_result;
long n_result;
    result=p_result=1;
    //这一段表达的斐波拉契数列第n项的值
    while(n>2)
    {
        n-=1;
        n_result=p_result;//把前一项的值赋给前一项的前一项
        p_result=result;  //
        result=p_result+n_result;//结果等于前一项加上前一项的前一项
        }

    return result;  
}
int main()
{
    for (int i = 1; i < 10; i++)
        cout << diedai(i) << endl;
}


Output:
1
2
3
4
5
6
7
8
9
1
1
2
3
5
8
13
21
34


Create a new paste based on this one


Comments: