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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7593355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:11:49  来源:igfitidea点击:

Program that uses while loops to calculate the first n Fibonacci numbers

c++while-loopputtyfibonacci

提问by Luis

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

当我运行它并输入一个数字时,它只会不停地重复它。例如,如果我放一个 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;
}

回答by NPE

There are several problems with the code:

代码有几个问题:

  1. You never assign anything to fibnuminside the body of the loop, so its value never changes.
  2. The purpose of current++is entirely unclear.
  1. 你永远不会fibnum在循环体内分配任何东西,所以它的值永远不会改变。
  2. 的目的current++完全不清楚。

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 currentand fibnum.

基本上,您需要确定每个变量的确切含义,并在整个. 这些变量的使用方式,显然混淆了current和的目的fibnum

回答by BLUEPIXY

#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;
}

回答by Jesus Ramos

change to

改成

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

回答by Jan S

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 循环,以便更轻松地计算数字

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

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

    cout << "," << current;
}

回答by Roman Kruglov

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:

除了先前的答案,请注意,如果您需要使用某个特定数字计算 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);
}

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.

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

回答by Kraken

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.

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

回答by Ben Voigt

#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

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

回答by user2758687

#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;
    }