C/C++学習:フィボナッチ数列


に質問
2週間の実訓、第2題:フィボナッチ数列
質問説明:キーボードからn(>2)を入力し、フィボナッチ数列の前のn項を計算し、行当たり10個のデータで出力します.
拡張問題:ある数がフィボナッチ数列にあるかどうかを検証します.
問題を解く
完全なコード:
1.キーボードからn(>2)を入力し、フィボナッチ数列の前のn項を計算し、行ごとに10個のデータを出力する.
/*========================================
Module Name:       
Module Date:20141222
Module Auth:CLyoko
Description:     n(>2),      
    n     10     。 

Other:
    Revision History:
    Date        Rel Ver.    Notes
    20141222    1.0              

==========================================*/

#include
#include

int main(int argc, char *argv[])
{
    int n=0;
    double fib[1000]={1,1};
    int i;
    printf("          
"
); printf(" ( 100):"); scanf("%d",&n); if(n>=2) { // for(i=2;i<=n;i++) fib[i]=fib[i-1]+fib[i-2]; // for(i=0;iprintf("%.0f\t",fib[i]); if((i+1)%10==0) printf("
"
); } } system("pause"); return 0; }

2.ある数がフィボナッチ数列にあるかどうかを検証します.
/*
Module Name:       
Module Date:20141223
Module Auth:CLyoko
Description:         

Other:
    Revision History:
    Date        Rel Ver.    Notes
    20141223    1.0              

*/

#include
#include

int main(int argc, char *argv[])
{
    int n=0;
    double fib[1000]={1,1};
    int i;
    printf("          
"
); printf(" ( 21):"); scanf("%d",&n); if(n>=0) { // for(i=2;i<=n+1;i++) { fib[i]=fib[i-1]+fib[i-2]; if(fib[0]==n) { printf("%d 。
"
,n); break; } else if(fib[i]==n) { printf("%d 。
"
,n); break; } else if(i==n+1) { printf("%d 。
"
,n); break; } } } system("pause"); return 0; }

転載は作者情報を保持してください.作者:CLyoko文章URL:http://blog.csdn.net/clyoko/article/details/43909163