【解題】ZeroJudge e208: 11541 – Decoding

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e208

#include <iostream>
using namespace std;

int main() {
    int T;
    string s;
    cin >> T;
    for (int Case = 1; Case <= T; Case++){
        cin >> s;
        cout << "Case " << Case << ": ";
        char ch = '\0';
        int num = 0;
        for (int i = 0; i < s.size(); i++){
            if (isalpha(s[i])){
                for (int j = 0; j < num; j++)
                    cout << ch;
                ch = s[i];
                num = 0;
            } else {
                num *= 10;
                num += s[i] - '0';
            }
        }
        while (num--) cout << ch;
        cout << "\n";
    }
    return 0;
}

Python code (credit: Amy Chou)

T = int(input())
for Case in range(T):
    s = list(input())
    ans = f"Case {Case+1}: "

    ch = s[0]
    idx = 1
    cnt = 0
    while idx < len(s):
        if ord(s[idx]) >= 48 and ord(s[idx]) <= 57:
            cnt = cnt*10 + int(s[idx])
        else:
            for j in range(cnt):
                ans += ch
            ch = s[idx]
            cnt = 0
        idx += 1

    for j in range(cnt):
        ans += ch
    print(ans)
分享本文 Share with friends