b024簡單

107 彰雲嘉區資訊學科能力競賽(2018)|統一發票對獎程式

依題目歷史表格計算每張發票最高獎。

解題思路

前五筆是固定八碼獎號,接著三碼資料是增開六獎;第一筆八碼資料開始才是待兌發票。用字串長度區分,保留前導零。

每張發票先判特別獎及特獎,再逐一比較三個頭獎的共同尾碼長度,按 8、7、6、5、4、3 位查獎金表。

額外三碼相符可得 200;全部條件取最大後才加入總額,因此同一張不重複領獎。

設發票數 n、增開號碼數 m,使用集合後時間 O(n+m),儲存 O(m);各號碼固定長度。

Step-by-step animation

最長共同尾碼與取最高獎|頭獎 12345678,發票 00345678。

回報動畫錯誤
步驟 1 / 3

設定獎號

頭獎 12345678,發票 00345678。

12345678
00345678
共同尾碼
6

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
int main() {
    string s, sp, pr;
    cin >> sp >> pr;
    vector<string> h(3);
    for (auto &x : h)
        cin >> x;
    set<string> extra;
    long long ans = 0;
    int pay[9] = {0, 0, 0, 200, 1000, 4000, 10000, 40000, 200000};
    while (cin >> s && s != "0") {
        if (s.size() == 3) {
            extra.insert(s);
            continue;
        }
        int best = 0;
        if (s == sp)
            best = 10000000;
        if (s == pr)
            best = max(best, 2000000);
        for (auto x : h) {
            int k = 0;
            while (k < 8 && s[7 - k] == x[7 - k])
                k++;
            best = max(best, pay[k]);
        }
        if (extra.count(s.substr(5)))
            best = max(best, 200);
        ans += best;
    }
    cout << ans << "\n";
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多