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
我正在尝试用 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