【问题标题】:Fibonacci Sequence and issues with return?斐波那契数列和回报问题?
【发布时间】:2014-08-30 23:32:56
【问题描述】:

我正在尝试学习已经开始使用 python 编程的 c++。这是一个计算两个值 a,b 之间的斐波那契数的简单程序。但是,当我运行代码时,只打印了数字 1,我不知道为什么。我认为这与在 for 循环中使用 return 有关。任何帮助将不胜感激

#include <iostream>
using namespace std;

int fibo(int b,int a=0){
    int x=0;
    int y=1;
    for(int i=0; i<=b; i++){
        int x0=x;
        int z=x+y;
        x=y;
        y=z;
        if(x>a){
            return x0;
        }
    }
}

int main()
{
    cout << fibo(100)<<endl;
    return 0;
}

这里是python函数,仅供参考

def fibo(b,a=0):
    x=0
    y=1
    while x<=b:
        z=x+y
        x0=x
        x=y
        y=z
        if x>a:
            print x0

我也在c++中尝试过以下方法

#include <iostream>
using namespace std;

int fibo(int b,int a=0){
    int x=0;
    int y=1;
    for(int i=0; i<=b; i++){
        int x0=x;
        int z=x+y;
        x=y;
        y=z;
        if(x>a){
            cout << x0 <<endl;
        }
    }
}

int main()
{
    fibo(100);
    return 0;
}

然而,这给出了超出 b 值的斐波那契数

【问题讨论】:

  • Python 函数打印许多值。 C++ 函数只返回一个。
  • 为什么用return代替了print? Python 代码正在打印每个值。 C++ 代码将返回第一个值,第一个值始终为 1。
  • 在你的python代码中,你使用x作为循环不变量,但在你的C++代码中,你使用i。所以这与 Python 的代码不同(显然)。
  • ohkk,我又检查了你的python代码。你的b在这里是什么?是要打印斐波那契的元素数量还是只是必须打印斐波那契的一个值?
  • @Arpit 斐波那契数被计算到 b (所以后者)

标签: python c++ fibonacci


【解决方案1】:

这是您的代码从 Python 到 C++ 的确切端口

#include <iostream>

using namespace std;

void fibo(int b,int a=0){
    int x=0;
    int y=1;
    int z, x0;
    while( x <= b ) {
        z= x + y;
        x0 = x;
        x = y;
        y = z;
        if(x > a) {
            cout << x0 << endl;
        }
    }
}

int main()
{
    fibo(100);
    return 0;
}

在您的 Python 代码中,如果没有显式返回,则函数的默认返回值为 None。在 C++ 中,这相当于 void function

为什么你的 for 循环不起作用?

for 循环被设计为迭代多次。它的语法是:

for (initialization; condition; increase) statement;

与 while 循环一样,此循环在条件为真时重复语句。但是,此外,for 循环提供了特定的 OPTIONAL 位置来包含初始化和增加表达式,分别在循环第一次开始之前和每次迭代之后执行。

  1. 执行初始化。通常,这声明了一个计数器 变量,并将其设置为某个初始值。这是执行 单次,在循环开始时。
  2. 检查条件。如果为真,则循环继续;除此以外, 循环结束,跳过语句,直接进入第 5 步。
  3. 语句已执行。像往常一样,它可以是单个语句 或用花括号 { } 括起来的块。
  4. increase 被执行,循环返回到第 2 步。
  5. 循环结束:执行后的下一条语句继续执行。

在此处阅读更多信息:http://www.cplusplus.com/doc/tutorial/control/#for

让我们分解你的循环:

int x=0;    // initialize x to 0
int y=1;    // initialize y to 1
for(        
  int i=0;  // initialize i to 0
  i<=b;     // keep looping until i is less than or equal to b (a variable passed in)
  i++       // after every single loop iteration, increment i by 1
) {  
    int x0=x;  // initialize x0 to x
    int z=x+y; // initialize z to (x + y)
    x=y;       // assign the value of y to x
    y=z;       // assign the value of z to y
    if(x>a){   // if x is greater than a, print the value of x0
        cout << x0 <<endl;
    }
}

在您的 Python 代码中,您没有i,您使用x 作为您的loop invariant。所以这应该是你的 for 循环的conditionx &lt;= b。初始化部分应该是你在循环之前设置的变量,所以:int x = 0, y = 1, x0, z 应该是initialization。最后一部分是增量。在您的python代码中,您的增量是x = y,但在for循环中,该部分在迭代完成后执行,所以我们不能只在增量部分设置x = yy = z 以来的 for 循环在增量部分之前执行。我们可以做的是使用一点代数:z = y + x,所以我们可以通过从z 中减去x 得到y 的值:z - x

这使得 for 循环:

void fibo2(int b,int a=0){
    for(
        int x = 0, y = 1, x0, z;
        x <= b;
        x = (z-x)
    ) {
        x0 = x;
        z = x+y;
        y = z;
        if(x > a){
            cout << x0 <<endl;
        }
    }
}

希望这会有所帮助。

【讨论】:

  • 这很好用,但我仍然在为为什么 for 循环没有给出相同的结果而苦苦挣扎。还是我将函数作为 int 类型而不是 void 的事实存在问题?
  • @Chris2807 不,这不是原因。原因是你在 C++ 中的 for 循环引入了一个 i,你用来阻止循环继续执行。让我在答案中详细说明一下。
  • 对不起,我现在意识到为什么 for 循环不起作用,那里的时间很慢。谢谢
【解决方案2】:

试试这个:

#include <iostream>
using namespace std;

int fibo(int b,int a=0){
    int x=0;
    int y=1;

    while(x<=b)
    {
        int z=x+y;
        int x0=x;
        x=y;
        y=z;
        if(x>a && x<b)
        {
            cout << x << " ";
        }
    }
}

int main()
{
    fibo(100);
    return 0;
}

http://ideone.com/225KIY

【讨论】:

    【解决方案3】:

    计算序列的常用数学(递归)方法limit(这不一定是最好/最有效的方法!):链接到演示here

    #include <iostream>
    using namespace std;
    
    int fibo(int x) 
    {
        if (x == 0) return 0;
        if (x == 1) return 1;
    
        return fibo(x-1)+fibo(x-2);
    }
    
    int main()
    {
    int j=1,limit=100;
    
        do
        {
        cout<< fibo(j) <<'\t';
        ++j;
        } while(fibo(j)<=limit);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2015-06-05
      • 2013-02-24
      • 2019-04-06
      • 2011-08-10
      相关资源
      最近更新 更多