【发布时间】:2018-09-09 12:25:39
【问题描述】:
它应该打印斐波那契数列直到一个位置,但它只打印 1 1 2,即使我要求的不仅仅是前三个元素。我该如何解决这个问题?
#include <iostream>
using std::cout;
using std::cin;
int main()
{
cout << "Enter a number: ";
int number;
cin >> number;
int count = 1;
int a = 1; //The first number of the Fibonacci's serie is 1
int b = 1; //The second number of the Fibonacci's serie is 2
while (count <= number)
{
if (count < 3)
cout << "1 ";
else
{
number = a + b; //Every number is the sum of the previous two
cout << number << " ";
if (count % 2 == 1)
a = number;
else
b = number;
}
count++;
}
return 0;
}
【问题讨论】: