Fibonacci数列(C++版)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SoftpaseFar/article/details/89602534

规律:

a[0] = 1;
a[1] = 1;
a[i] = a[i-2] + a[i-1];

代码演示:

#include <iostream>
using namespace std;

int main() {
  int N;
        printf("Please input the number N:");
        scanf("%d",&N);
        while(N<2){
            cout<<"Please input again:";
            cin>>N;
        }
        int a[N];
        a[0] = 1;
        a[1] = 1;
        for(int i = 2;i<N;i++){
            a[i] = a[i-2] + a[i-1];
        }

        for(int j = 0;j<N;j++){
            cout<<a[j]<<"\t";
        }
        return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/SoftpaseFar/article/details/89602534