每个Fibonacci数

时间:2015-09-23 23:18:01

标签: c++ recursion counter

为什么我的代码输出中的数字与预期的输出结果(最后粘贴)之间存在差异?

我经历了与计算递归调用次数有关的这些问题:

我无法对我的问题形成正确的答案或解决方案。

我甚至尝试通过在for循环之后在main()函数中写call_count = 0;或者在函数定义中(如上面第二个链接的答案中所建议的)写入#include <iostream> #include "math.h" #include <iomanip> using namespace std; int call_count = 0; int fibo(int n) { call_count+=1; if(n<=2) { return 1; } else { //call_count += 1; return fibo(n-1) + fibo(n-2); } return n; } int main() { int num; cout<<"\nenter the number of integers to be printed in the fibonacci series\n"; cin>>num; cout<<"\nfibonacci series for first "<<num<<" numbers is\n"; cout<<"\n\nSerial Number\t"<<"FIBO_NUMBER\t"<<" NO_OF_CALLS MADE\n\n"; for(int i=1;i<=num;i++) { cout<<endl<<i<<"th number\t "<<fibo(i)<<"\t\t"<<call_count<<" calls\n"; } cout<<endl<<"\n the total number of recursive calls made were "<<call_count<<endl<<endl; system("pause"); return 0; } 来重新初始化计数,但是,它确实不工作或给我预期的输出。

我已将下面的代码粘贴在代码下面,并将其输出粘贴到代码下方,并将预期输出粘贴到代码输出下方。

enter the number of integers to be printed in the fibonacci series
15

fibonacci series for first 15 numbers is

Serial Number   FIBO_NUMBER      NO_OF_CALLS MADE


1th number         1            0 calls

2th number         1            1 calls

3th number         2            2 calls

4th number         3            5 calls

5th number         5            10 calls

6th number         8            19 calls

7th number         13           34 calls

8th number         21           59 calls

9th number         34           100 calls

10th number        55           167 calls

11th number        89           276 calls

12th number        144          453 calls

13th number        233          740 calls

14th number        377          1205 calls

15th number        610          1958 calls


 the total number of recursive calls made were 3177

Press any key to continue . . .

我的代码的输出:

1 th integer of fibonacci series is 1 and it needed 0 recursive calls
2 th integer of fibonacci series is 1 and it needed 0 recursive calls
3 th integer of fibonacci series is 2 and it needed 2 recursive calls
4 th integer of fibonacci series is 3 and it needed 4 recursive calls
5 th integer of fibonacci series is 5 and it needed 8 recursive calls
6 th integer of fibonacci series is 8 and it needed 14 recursive calls
7 th integer of fibonacci series is 13 and it needed 24 recursive calls
8 th integer of fibonacci series is 21 and it needed 40 recursive calls
9 th integer of fibonacci series is 34 and it needed 66 recursive calls
10 th integer of fibonacci series is 55 and it needed 108 recursive calls
11 th integer of fibonacci series is 89 and it needed 176 recursive calls
12 th integer of fibonacci series is 144 and it needed 286 recursive calls
13 th integer of fibonacci series is 233 and it needed 464 recursive calls
14 th integer of fibonacci series is 377 and it needed 752 recursive calls
15 th integer of fibonacci series is 610 and it needed 1218 recursive calls
Press any key to continue . . .

EXPECTED输出数字如下:

extraçao

如何解决这种不匹配问题?

2 个答案:

答案 0 :(得分:3)

在调用fibo()方法之前将call_count重置为零。

#include <iostream>
#include "math.h"
#include <iomanip>

using namespace std;

int call_count = 0;

int fibo(int n)
{
    call_count+=1;
    if(n<=2)
    {
        return 1;
    }
    else {
            //call_count += 1;
            return fibo(n-1) + fibo(n-2);
         }
    return n;
}

int main()
{
    int num;
    cout<<"\nenter the number of integers to be printed in the fibonacci series\n";
    cin>>num;
    cout<<"\nfibonacci series for first "<<num<<" numbers is\n";
    cout<<"\n\nSerial Number\t"<<"FIBO_NUMBER\t"<<" NO_OF_CALLS MADE\n\n";
    for(int i=1;i<=num;i++)
    {
        call_count = 0; 
        cout<<endl<<i<<"th number\t   "<<fibo(i)<<"\t\t"<<call_count<<" calls\n";
    }
    cout<<endl<<"\n the total number of recursive calls made were "    <<call_count<<endl<<endl;
    system("pause");
    return 0;
}

答案 1 :(得分:0)

总结@rlbond和@zeroCool的精确输入,以及两者的微小变化,形成一个答案: -

在每次循环迭代时将call_count重置为零,并在调用fibo的单独语句上打印call_count(因为它可以在调用点进行评估),将产生预期的输出。 还需要从call_count中减去1(对于初始调用),同时打印出每个斐波纳契数的count语句以获得预期的数量。

以下代码减少了额外变量的需要,并将print语句评估分成预期输出。 (或者可以参考rlbond对上述问题的评论中的链接。ideone.com/8EWjOC

#include <iostream>
#include "math.h"
#include <iomanip>

using namespace std;

int call_count = 0;

int fibo(int n)
{
    call_count+=1;
    if(n<=2)
    {
        return 1;
    }
    else {
            return fibo(n-1) + fibo(n-2);
         }
    return n;
}

int main()
{
    int num;
    cout<<"\nenter the number of integers to be printed in the fibonacci series\n";
    cin>>num;
    cout<<"\nfibonacci series for first "<<num<<" numbers is\n";
    cout<<"\n\nSerial Number\t"<<"FIBO_NUMBER\t"<<" NO_OF_CALLS MADE\n\n";
    for(int i=1;i<=num;i++)
    {
        call_count = 0;
        cout<<endl<<i<<"th number\t   "<<fibo(i)<<"\t\t";
        cout<<call_count-1<<" calls\n";
    }
cout<<endl<<"\n the total number of recursive calls made were "<<call_count-1<<endl<<endl;
system("pause");
return 0;
}


工作正常,输出如下: -

输入斐波那契系列中要打印的整数数 15

前15个数字的斐波纳契数列是

序列号FIBO_NUMBER NO_OF_CALLS MADE

第1个号码1 0电话

第2个号码1 0电话

第3个号码2 2个电话

第4个号码3 4个电话

第5个号码5 8个电话

第6个号码8 14个电话

第7个号码13 24个电话

第8个号码21 40个电话

第9个号码34 66个电话

第10个号码55 108个电话

第11个号码89 176个电话

第12个号码144 286个电话

第13个号码233 464个电话

第14号377 752电话

第15个号码610 1218电话

递归调用的总数为1218

按任意键继续。 。

感谢每个人的贡献。