嘗試用 tuple 實作結果踩坑,因為 tuple 沒有保證 call function 的順序性。

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
37
38
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

ll solve()
{
    char inp[4 + 3];
    scanf("%s", inp);

    if (inp[0] == 'f') {
        ll x = solve();
        return 2 * x - 3;
    } else if (inp[0] == 'g') {
        ll x = solve();
        ll y = solve();
        return 2 * x + y - 7;
    } else if (inp[0] == 'h') {
        // ll x, y, z;
        // tie(x, y, z) = make_tuple(solve(), solve(), solve()); // function call ordering is not guaranteed for solve()
        
        ll x = solve();
        ll y = solve();
        ll z = solve();
        return 3 * x - 2 * y + z;
    } else {
        return atoi(inp);
    }
}

int main()
{
    printf("%lld\n", solve());

    return 0;
}