题解:SP94 MAYA - Numeral System of the Maya

· · 题解

题目大意

将一个特殊进制的数转换为十进制。

思路

对于当前这一位,. 表示 1、- 表示 5。我们可以遍历当前这一位并统计答案。

对于每一位的基数,不难发现:除了第 3 位,其余的都是前一位的 20 倍,因此可以得出以下结论。

w_x = \begin{cases} 1 & x = 1 \\ 20 & x = 2 \\ 18 \times 20^{x-2} & x < 2 \end{cases}

因此,答案就是每一位的值乘上当前这一位的基数再求和。

代码

#include <bits/stdc++.h>
#define int long long

using namespace std;

int n;
string s;

int num(string x) {
    if (x == "S") return 0;
    int cnt1 = 0, cnt2 = 0;
    for (char c : x) {
        if (c == '.') cnt1++;
        if (c == '-') cnt2++;
    }
    return cnt1 + cnt2 * 5;
}

int w(int x) {
    if (x == 1) return 1;
    if (x == 2) return 20;
    return 18 * pow(20, x - 2);
}

signed main() {
    while (cin >> n, n) {
        getline(cin, s);
        int ans = 0;
        for (int i = n; i; i--) {
            getline(cin, s);
            ans += num(s) * w(i);
        }
        cout << ans << endl;
    }
    return 0;
}