【问题标题】:Fibonacci Sequence Generator in C++C++ 中的斐波那契数列生成器
【发布时间】:2015-05-28 01:18:32
【问题描述】:

我正在尝试用 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 进行编译,运行程序时似乎没有显示任何输出。如果有人对为什么会发生这种情况有任何想法,或者我的程序有任何逻辑缺陷,请提供帮助。谢谢。

【问题讨论】:

  • &lt;&lt;std::endl' after you didsplay x` 放置会有所帮助,这样您实际上可以在新行上显示内容。检查您的项目参数以确保您正在制作控制台程序

标签: c++ fibonacci


【解决方案1】:

C++ 不是 Python,编译器会忽略空格。 while 循环必须使用大括号,

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

【讨论】:

  • 非常感谢!我不太习惯这种语法,并且修复了它。
猜你喜欢
  • 2011-12-18
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2013-05-04
相关资源
最近更新 更多