熱門文章

2026年5月9日 星期六

ZeroJudge 解題筆記:b982. YoJudge 預練(空間之章)

作者:王一哲
日期:2026年5月9日


ZeroJudge 題目連結:b982. YoJudge 預練(空間之章)

解題想法


這題我是用最直接的寫法,先找出字串 s 中 g, m, k, byte, bit, . 的索引值,再寫很多層的 if, else 判斷 s 的格式,依照格式分割字串、換算答案。

Python 程式碼


使用時間約為 14 ms,記憶體約為 8.6 MB,通過測試。
while True:
    try:
        s = input()
        ans = 0
        if "gb" in s:
            i = s.find("gb")
            a = int(s[:i])
            ans = 8*a*10**9
        elif "mb" in s:
            i = s.find("mb")
            a = int(s[:i])
            ans = 8*a*10**6
        elif "kb" in s:
            i = s.find("kb")
            if "." in s:
                j = s.find(".")
                a = int(s[:j])
                b = int(s[j+1:i])
                ans = 8*(a*10**3 + b*100)
            else:
                a = int(s[:i])
                ans = 8*a*10**3
        elif "byte" in s:
            i = s.find("byte")
            if "." in s:
                j = s.find(".")
                a = int(s[:j])
                b = int(s[j+1:i])
                ans = 8*a + b
            else:
                ans = 8*int(s[:i])
        elif "bit" in s:
            i = s.find("bit")
            ans = int(s[:i])
        elif "g" in s and "m" in s and "k" in s:
            i = s.find("g")
            j = s.find("m")
            k = s.find("k")
            a = int(s[:i])
            b = int(s[i+1:j])
            c = int(s[j+1:k])
            ans = 8*(a*10**9 + b*10**6 + c*10**3)
        elif "g" in s and "m" in s:
            i = s.find("g")
            j = s.find("m")
            a = int(s[:i])
            b = int(s[i+1:j])
            ans = 8*(a*10**9 + b*10**6)
        elif "m" in s and "k" in s:
            i = s.find("m")
            j = s.find("k")
            a = int(s[:i])
            b = int(s[i+1:j])
            ans = 8*(a*10**6 + b*10**3)
        print(ans)
    except EOFError:
        break

使用時間約為 15 ms,記憶體約為 8.6 MB,通過測試。
while True:
    try:
        s = input()
        # 指定字元於 s 之中的索引值,如果沒有找到會回傳 -1
        gb = s.find("g")
        mb = s.find("m")
        kb = s.find("k")
        byte = s.find("byte")
        bit = s.find("bit")
        dot = s.find(".")
        # 答案
        ans = 0
        # 依照有找到的字元換算答案
        if gb != -1 and mb == -1 and kb == -1:  # 只有 gb
            ans = 8 * int(s[:gb])*10**9
        elif mb != -1:  # 有找到 mb
            if gb != -1 and kb != -1:  # 同時有 g, m, k
                a = int(s[:gb])
                b = int(s[gb+1:mb])
                c = int(s[mb+1:kb])
                ans = 8 * (a*10**9 + b*10**6 + c*1000)
            elif gb != -1 and kb == -1:  # 同時有 g, m
                a = int(s[:gb])
                b = int(s[gb+1:mb])
                ans = 8 * (a*10**9 + b*10**6)
            elif gb == -1 and kb != -1:  # 同時有 m, k
                a = int(s[:mb])
                b = int(s[mb+1:kb])
                ans = 8 * (a*10**6 + b*1000)
            elif gb == -1 and kb == -1:  # 只有 m
                ans = 8*int(s[:mb])*10**6
        elif kb != -1 and gb == -1 and mb == -1:  # 只有 k
            if dot != -1:  # 有小數點
                a = int(s[:dot])
                b = int(s[dot+1:kb])
                ans = 8 * (a*1000 + b*100)
            else:  # 沒有小數點
                ans = 8 * int(s[:kb])*1000
        elif byte != -1:  # 有 byte
            if dot != -1:  # 有小數點
                a = int(s[:dot])
                b = int(s[dot+1:byte])
                ans = 8*a + b
            else:  # 沒有小數點
                ans = 8 * int(s[:byte])
        elif bit != -1:  # 有 bit
            ans = int(s[:bit])
        # 印出答案
        print(ans)
    except EOFError:
        break


C++ 程式碼


使用時間約為 3 ms,記憶體約為 3.4 MB,通過測試。
#include <iostream>
#include <string>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    string s;
    while(getline(cin, s)) {
        // 指定字元於 s 之中的索引值,如果沒有找到設為 string::npos
        size_t gb = s.find("g"), mb = s.find("m"), kb = s.find("k");
        size_t byte = s.find("byte"), bit = s.find("bit"), dot = s.find(".");
        // 答案
        size_t ans = 0;
        // 依照有找到的字元換算答案
        if (gb != string::npos && mb == string::npos && kb == string::npos) {  // 只有 gb
            ans = 8 * stol(s.substr(0, gb)) * 1000000000;
        } else if (mb != string::npos) {  // 有找到 mb
            if (gb != string::npos && kb != string::npos) {  // 同時有 g, m, k
                size_t a = stol(s.substr(0, gb));
                size_t b = stol(s.substr(gb+1, mb - gb - 1));
                size_t c = stol(s.substr(mb+1, kb - mb - 1));
                ans = 8 * (a*1000000000 + b*1000000 + c*1000);
            } else if (gb != string::npos && kb == string::npos) {  // 同時有 g, m
                size_t a = stol(s.substr(0, gb));
                size_t b = stol(s.substr(gb+1, mb - gb - 1));
                ans = 8 * (a*1000000000 + b*1000000);
            } else if (gb == string::npos && kb != string::npos) {  // 同時有 m, k
                size_t a = stol(s.substr(0, mb));
                size_t b = stol(s.substr(mb+1, kb - mb - 1));
                ans = 8 * (a*1000000 + b*1000);
            } else if (gb == string::npos && kb == string::npos) {  // 只有 m
                ans = 8*stol(s.substr(0, mb))*1000000;
            }
        } else if (kb != string::npos && gb == string::npos && mb == string::npos) {  // 只有 k
            if (dot != string::npos) {  // 有小數點
                size_t a = stol(s.substr(0, dot));
                size_t b = stol(s.substr(dot+1, kb - dot - 1));
                ans = 8 * (a*1000 + b*100);
            } else {  // 沒有小數點
                ans = 8 * stol(s.substr(0, kb))*1000;
            }
        } else if (byte != string::npos) {  // 有 byte
            if (dot != string::npos) {  // 有小數點
                size_t a = stol(s.substr(0, dot));
                size_t b = stol(s.substr(dot+1, byte - dot - 1));
                ans = 8*a + b;
            } else {  // 沒有小數點
                ans = 8 * stol(s.substr(0, byte));
            }
        } else if (bit != string::npos) {  // 有 bit
            ans = stol(s.substr(0, bit));
        }
        // 印出答案
        cout << ans << "\n";
    }
    return 0;
}


沒有留言:

張貼留言