簡體   English   中英

C ++中的斐波那契序列生成器

[英]Fibonacci Sequence Generator in C++

我試圖用C ++制作斐波那契序列生成器,但遇到了一些麻煩。 我是C ++的新手,正嘗試用我所知道的其他語言來完成這項工作。 所以我寫了一個程序:

#include <iostream>

using namespace std;

int NextNumber(int x, int y, int z)
{
    z=x+y;
    return z;
}

int main()
{
    int q;
    int w;
    int x=0;
    int y=1;
    int z=1;
    int t=1;
    cout << x;
    cout << ", "<< y ;
    cout << ", "<< z ;
    while (t<10)
        x=NextNumber(y,z,x);
        cout << ", "<< x ;
        q=y; //Q is farthest back
        w=z; //W is in the middle
        z=x; //Z moves from middle to front
        y=w; //Y moves from back to middle
        x=q; //X moves from front to back
        t=t+1; //Add a rotation to the while loop
}

我正在使用Xcode進行編譯,運行該程序時似乎沒有輸出顯示。 如果有人對為什么會發生這種情況有任何想法,或者我的程序有任何邏輯缺陷,請提供幫助。 謝謝。

C ++不是Python,編譯器會忽略空格。 您必須在while循環中使用花括號,

while (t<10)
{
  // details
} // end loop 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM