C++生成斐波拉其数列

该方法作为一种演示功能左右,运行较慢。

#include <iostream>

using namespace std;

class Fibonacci{
public:
    int a, b, c;
    void generate(int);
};

void Fibonacci::generate(int n){
    a = 0; b = 1;
    cout << a << " " <<b;
    for(int i=1; i<= n-2; i++){
        c = a + b;
        cout << " " << c;
        a = b;
        b = c;
    }
}

int main()
{
    cout << "Hello world! Fibonacci series" << endl;
    cout << "Enter number of items you need in the series: ";
    int n;
    cin  >> n;
    Fibonacci fibonacci;
    fibonacci.generate(n);
    return 0;
}

 

posted @ 2019-01-21 20:54  立体风  阅读(549)  评论(0编辑  收藏  举报