80721 - 同测活动第4场阅读程序2
统计题目(材料题)
(2)
01 #include <iostream>
02 using namespace std;
03 int f_rec(int n) {
04 if (n == 0) return 1;
05 if (n == 1) return 2;
06 return f_rec(n-1) + 2 * f_rec(n-2);
07 }
08 int f_iter(int n) {
09 int a = 1, b = 2;
10 if (n == 0) return a;
11 if (n == 1) return b;
12 int c;
13 for (int i = 2; i <= n; i++) {
14 c = b + 2 * a;
15 a = b;
16 b = c;
17 }
18 return b;
19 }
20 int main() {
21 int n;
22 cin >> n;
23 int r = f_rec(n);
24 int it = f_iter(n);
25 cout << r << " " << it;
26 return 0;
27 }
假设输入的 n 为非负整数,且 n ≤ 30,完成下面的判断题和单选题。
||

关注我们