使用 while 循环计算前 n 个斐波那契数的程序

发布于 2024-12-07 06:58:33 字数 493 浏览 1 评论 0原文

当我运行它并输入一个数字时,它只是不停地重复它。例如,如果我输入 3,它将执行此操作 3 3 3 3 3 但永不停歇

int main()
{
int current=0, prev=1, prev2=1, fibnum;
cout << "Enter the number of Fibonacci numbers to compute: ";
cin >> fibnum;
if (fibnum <=0)
{
    cout << "Error: Enter a positive number: ";
}
while (fibnum > 0){
    current = prev + prev2;
    prev = prev2;
    prev2 = current;
    current++;

    cout << "," << fibnum;
    cout << endl;
}
return 0;
}

When I run it and input a number it just repeats it over non-stop. for example if i put a 3 it will do this
3
3
3
3
3
BUT NON STOP

int main()
{
int current=0, prev=1, prev2=1, fibnum;
cout << "Enter the number of Fibonacci numbers to compute: ";
cin >> fibnum;
if (fibnum <=0)
{
    cout << "Error: Enter a positive number: ";
}
while (fibnum > 0){
    current = prev + prev2;
    prev = prev2;
    prev2 = current;
    current++;

    cout << "," << fibnum;
    cout << endl;
}
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

把梦留给海 2024-12-14 06:58:33

该代码存在几个问题:

  1. 您从未在循环体内向 fibnum 分配任何内容,因此它的值永远不会改变。
  2. current++ 的目的完全不清楚。

基本上,您需要确定每个变量的确切含义,并始终坚持它。从这些变量的使用方式来看,currentfibnum 的用途显然存在混淆。

There are several problems with the code:

  1. You never assign anything to fibnum inside the body of the loop, so its value never changes.
  2. The purpose of current++ is entirely unclear.

Basically, you need to decide on the exact meaning of every variable, and stick to it throughout. The way these variables are being used, there's clearly confusion around the purpose of current and fibnum.

千纸鹤带着心事 2024-12-14 06:58:33
#include <iostream>

using namespace std;

int main(){
    int current=0, prev=0, prev2=1, fibnum;
    cout << "Enter the number of Fibonacci numbers to compute: ";
    cin >> fibnum;
    if (fibnum <=0){
        cout << "Error: Enter a positive number: ";
    }
    while (fibnum--){
        cout << prev ;
        current = prev + prev2;
        prev = prev2;
        prev2 = current;
        if(fibnum)
            cout << ",";
    }
    cout << endl;
    return 0;
}
#include <iostream>

using namespace std;

int main(){
    int current=0, prev=0, prev2=1, fibnum;
    cout << "Enter the number of Fibonacci numbers to compute: ";
    cin >> fibnum;
    if (fibnum <=0){
        cout << "Error: Enter a positive number: ";
    }
    while (fibnum--){
        cout << prev ;
        current = prev + prev2;
        prev = prev2;
        prev2 = current;
        if(fibnum)
            cout << ",";
    }
    cout << endl;
    return 0;
}
戏舞 2024-12-14 06:58:33

更改为

int current_fib_num = 0;
....
while (current_fib_num++ != fibNum)
{
    ....
    // your code here
}

change to

int current_fib_num = 0;
....
while (current_fib_num++ != fibNum)
{
    ....
    // your code here
}
雪落纷纷 2024-12-14 06:58:33

除了前面的答案之外,请注意,如果您需要计算具有某个特定数字的 fib 数,您可以使用递归的好处。类似的事情:

#include <cstddef>

std::size_t fib( std::size_t num )
{
    // For first two numbers
    if (num <= 2)
        return 1;

    return fib(num - 1) + fib(num - 2);
}

但您必须记住,这将导致冗余计算,因为相同数字的可重复重新计算以及用于传输函数参数的堆栈使用。

In addition to previous answers note that you can use benefits of recursion if you need to calculate fib number with some certain number. Something like that:

#include <cstddef>

std::size_t fib( std::size_t num )
{
    // For first two numbers
    if (num <= 2)
        return 1;

    return fib(num - 1) + fib(num - 2);
}

but you must keep in mind that this will lead to redundant calculations 'coz of repeatable recalc of same numbers and stack use for transmitting function args.

甜味拾荒者 2024-12-14 06:58:33

您正在尝试打印 fibnum,但它在 while 循环内没有改变。
您应该打印 current 。
您还需要设置一个计数器来查看 while 循环的结束。

You are trying to print fibnum, but it is not changing inside the while loop.
You should be printing current instead.
Also you need to set a counter that will see the end of while loop.

终止放荡 2024-12-14 06:58:33
#include <iostream>

using std::cin;
using std::cout;

int main()
{
  int a=1, b=1, nums_to_print;
  while (1) {
    cout << "Enter the number of Fibonacci numbers to compute: ";
    cin >> nums_to_print;
    if (nums_to_print > 0) {
      while (1) {
        cout << a;
        b += a;
        a = b - a;
        if (--nums_to_print) cout << ",";
        else break;
      }
      cout << "\n";
      return 0;
    }

    cout << "Error: Enter a positive number.\n";
  }
}

演示:http://ideone.com/3H8Fq

#include <iostream>

using std::cin;
using std::cout;

int main()
{
  int a=1, b=1, nums_to_print;
  while (1) {
    cout << "Enter the number of Fibonacci numbers to compute: ";
    cin >> nums_to_print;
    if (nums_to_print > 0) {
      while (1) {
        cout << a;
        b += a;
        a = b - a;
        if (--nums_to_print) cout << ",";
        else break;
      }
      cout << "\n";
      return 0;
    }

    cout << "Error: Enter a positive number.\n";
  }
}

Demo: http://ideone.com/3H8Fq

彻夜缠绵 2024-12-14 06:58:33

您需要解决一些问题。

你需要有一个计数变量;

int current=0, prev=0, prev2=1, fibnum;

int count;

....

要在循环之前输出第一个数字

cout<<prev2;

您可以将其更改为 for 循环,以便更轻松地计算数字

for(count = 0; count <= fibnum; count++){
    current = prev + prev2;
    prev = prev2;
    prev2 = current;

您需要打印 current,而不是 fibnum -> fibnum 是您需要打印的总数

    cout << "," << current;
}

There are a couple of things you need to fix.

You need to have a count variable;

int current=0, prev=0, prev2=1, fibnum;

int count;

....

To output the first number before the loop

cout<<prev2;

You can change this to a for loop to make it easier to count the numbers

for(count = 0; count <= fibnum; count++){
    current = prev + prev2;
    prev = prev2;
    prev2 = current;

You need to print current, not fibnum -> fibnum is the total numbers that you need to print

    cout << "," << current;
}
病毒体 2024-12-14 06:58:33
#include <iostream>

using namespace std;

    int main()
    {
         int i=0,j=1;
        int c,n,count=0,d;
        cout<<"enter num";
        cin>>n;
        c=i+j;
        cout<<i<<j;
    while(count<n-2)
        {   d=j+c;
            cout<<d;
            j=c;
            c=d;
            count++;
        }
         return 0;
    }
#include <iostream>

using namespace std;

    int main()
    {
         int i=0,j=1;
        int c,n,count=0,d;
        cout<<"enter num";
        cin>>n;
        c=i+j;
        cout<<i<<j;
    while(count<n-2)
        {   d=j+c;
            cout<<d;
            j=c;
            c=d;
            count++;
        }
         return 0;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文