📜  算法测验|须藤放置[1.5] |问题7

📅  最后修改于: 2021-06-29 23:19:38             🧑  作者: Mango

以下是用于打印斐波那契数列的不完整代码。可以在“要插入的代码”语句中插入哪个语句,以使代码可以完美运行?

// CPP Program to print Fibonacci sequence
// using 2 variables
#include 
using std::cout;
void fib(int n)
{
    int a = 0, b = 1;
    if (n >= 0)
        cout << a << " ";
    if (n >= 1)
        cout << b << " ";
    for (int i = 2; i <= n; i++) {
        cout << a + b << " ";
        // code to be inserted here
  
    }
}
   
// Driver code
int main()
{
    fib(9);
    return 0;
}

(A) b = a – b; b = b + a;
(B) b = a + b; a = a – b;

(C) b = a + b; a = b – a;
(D)这些都不是。答案: (C)
说明:请参阅:https://www.geeksforgeeks.org/print-fibonacci-sequence-using-2-variables/
这个问题的测验