下面代码采用递推算法来计算斐波那契数列 ,则横线上应填写( )。
int fib(int n) {
if (n == 0 || n == 1)
return n;
int f1 = 0;
int f2 = 1;
int result = 0;
for (int i = 2; i <= n; i++) {
________________________________ // 在此处填入代码
}
return result;
}
- A. 1 result = f1 + f2; 2 f1 = f2; 3 f2 = result;
- B. 1 result += f1 + f2; 2 f1 = f2; 3 f2 = result;
- C. 1 result += f1 + f2; 2 f2 = result; 3 f1 = f2;
- D. 1 result = f1 + f2; 2 f2 = result; 3 f1 = f2;
正确答案:A