寫到無地自容的一題…

遞迴都寫不出來 QQ

AC Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

ll solve(char *tok)
{
    if (tok[0] == 'f') {
        tok = strtok(NULL, " ");
        int x = solve(tok);

        return 2 * x - 1;
    } else if (tok[0] == 'g') {
        tok = strtok(NULL, " ");
        int x = solve(tok);
        tok = strtok(NULL, " ");
        int y = solve(tok);

        return x + 2 * y - 3;
    } else {
        return atoi(tok);
    }
}

int main()
{
    char inp[1111];
    fgets(inp, 1111, stdin);

    char *tok = strtok(inp, " ");
    printf("%lld\n", solve(tok));

    return 0;
}