不是VIP会员,不能显示答案

题目解答

题目:
#include <iostream> 
using namespace std; 
int fun(int n, int fromPos, int toPos) {
     int t, tot;
     if (n == 0)
         return 0; 
    for (t = 1; t <= 3; t++) 
        if (t != fromPos && t != toPos)
             break;
     tot = 0; 
    tot += fun(n - 1, fromPos, t);
     tot++; 
    tot += fun(n - 1, t, toPos);
     return tot;
 } 
int main() {
     int n;
     cin >> n; 
    cout << fun(n, 1, 3) << endl;
     return 0;
 }
输入:5
输出:31
考点: 0
分析:
解答: 仔细观察函数内容可以发现函数中的fromPos和toPos并没有什么卵用,所以不用管这两个变量直接求,答案是2^5−1=31。
评论:
老师: 0