题库 C++/C语言题库 题目列表 小杨正在爬楼梯,需要爬n阶才能到达楼顶。如果每次可...
单选题

小杨正在爬楼梯,需要爬n阶才能到达楼顶。如果每次可以爬1个或2个台阶,下面代码采用递推算法来计算一共有多少种不同的方法可以爬到楼顶,则横线上应填写( )。

1 int f(int n) {
2  if (n == 1 || n == 2) 
3   return n; 
4
5  int f1 = 1; 
6  int f2 = 2; 
7  int res = 0; 
8  for (int i = 3; i <= n; i++) { 
9   ________________________________ // 在此处填入代码 
10  } 
11  return res; 
12 }

A.

1 res += f1 + f2; 
2 f1 = f2; 
3 f2 = res;
B.

1 res = f1 + f2; 
2 f1 = f2; 
3 f2 = res;
C.

1 res += f1 + f2; 
2 f2 = res; 
3 f1 = f2;
D.

1 res = f1 + f2; 
2 f2 = res; 
3 f1 = f2;
题目信息
2025年 四级 选择题
-
正确率
0
评论
24
点击