试图在 C++ 中的斐波那契循环中获得低于输入数字的斐波那契数
Trying to get a fibonacci numbers lower than a input number in a fibonacci loop in c++
我正在尝试构建一个程序,要求用户输入一个数字 "k" 并打印一系列小于数字 k 的数字。
例如,如果用户写 20
输出必须是:
0, 1, 1, 2, 3, 5, 8, 13
相反,我得到了:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
这意味着正在打印 20 个斐波那契数。
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
unsigned int fib(unsigned int num)
{
if (num == 0 || num == 1)
{
return num;
}
else
{
return (fib(num - 1) + fib(num - 2));
}
}
int main()
{
int k = 0;
cout << "White a number k ";
cin >> k;
for (int i = 0; i < k; i++)
cout << fib(i) << endl;
return 0;
}
我试过使用 "break;" 但它不起作用。我怎样才能打破循环?
for (int i=0; i<k; i++){
if(fib(i)<k) //while fib(i) is less than k,it prints fib(i)
cout << fib(i) << endl;
else //if number becomes greater than k,then it will break out from the loop.
break;
}
希望对您有所帮助!!
您正在将输入的 k
限制与迭代器 i
进行比较,因此输入被读取为您要输出的斐波那契数列中的数字数量,而不是上限。
要执行您想要的操作,您需要将限制 k
与 fib
函数返回的结果进行比较,以便您可以在超过限制时立即停止循环。
保持 for
循环:
int main()
{
int k = 0;
int res = 0; // store the result of fib(i) to avoid duplicate calls
cout << "White a number k: ";
cin >> k;
for (int i = 0; (res = fib(i)) < k; i++){ //assign fib(i) to res
cout << res << endl; //when res is larger than k exits the loop
}
return 0;
}
或使用 while
循环:
int main()
{
int k = 0;
cout << "White a number k: ";
cin >> k;
int res;
int i = 0; //iterator
while((res = fib(i++)) < k) { //assign result to res and compare it with the limit k
cout << res << endl; //output the result while it's smaller than k
}
return 0;
}
样本运行:
White a number k: 20
0
1
1
2
3
5
8
13
我正在尝试构建一个程序,要求用户输入一个数字 "k" 并打印一系列小于数字 k 的数字。
例如,如果用户写 20
输出必须是:
0, 1, 1, 2, 3, 5, 8, 13
相反,我得到了:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181
这意味着正在打印 20 个斐波那契数。
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
unsigned int fib(unsigned int num)
{
if (num == 0 || num == 1)
{
return num;
}
else
{
return (fib(num - 1) + fib(num - 2));
}
}
int main()
{
int k = 0;
cout << "White a number k ";
cin >> k;
for (int i = 0; i < k; i++)
cout << fib(i) << endl;
return 0;
}
我试过使用 "break;" 但它不起作用。我怎样才能打破循环?
for (int i=0; i<k; i++){
if(fib(i)<k) //while fib(i) is less than k,it prints fib(i)
cout << fib(i) << endl;
else //if number becomes greater than k,then it will break out from the loop.
break;
}
希望对您有所帮助!!
您正在将输入的 k
限制与迭代器 i
进行比较,因此输入被读取为您要输出的斐波那契数列中的数字数量,而不是上限。
要执行您想要的操作,您需要将限制 k
与 fib
函数返回的结果进行比较,以便您可以在超过限制时立即停止循环。
保持 for
循环:
int main()
{
int k = 0;
int res = 0; // store the result of fib(i) to avoid duplicate calls
cout << "White a number k: ";
cin >> k;
for (int i = 0; (res = fib(i)) < k; i++){ //assign fib(i) to res
cout << res << endl; //when res is larger than k exits the loop
}
return 0;
}
或使用 while
循环:
int main()
{
int k = 0;
cout << "White a number k: ";
cin >> k;
int res;
int i = 0; //iterator
while((res = fib(i++)) < k) { //assign result to res and compare it with the limit k
cout << res << endl; //output the result while it's smaller than k
}
return 0;
}
样本运行:
White a number k: 20
0
1
1
2
3
5
8
13