熱門文章

2026年3月19日 星期四

ZeroJudge 解題筆記:c127. 00537 - Artificial Intelligence

作者:王一哲
日期:2026年3月19日


ZeroJudge 題目連結:c127. 00537 - Artificial Intelligence

解題想法


我用字典儲存前綴詞及單位對應的倍率,從讀取到的字串之中搜尋 I=、U=、P= 這 3 個字串,如果有找到這 3 個字串,再從字串索引值加 2 的位置開始向後讀取字元,找出對應的數字。最後配合前綴詞及單位調整數值的大小,輸出答案。

Python 程式碼


用時間約為 7 ms,記憶體約為 3 MB,通過測試。
prefix = {'m': 0.001, 'k': 1000, 'M': 1000000, 'A': 1, 'V': 1, 'W': 1}  # 前綴詞及單位對應的倍率
T = int(input())  # T 組測資
for t in range(1, T+1):  # t = 1 ~ T
    if t > 1: print()  # 如果不是第 1 組測資,先換行
    print(f"Problem #{t:d}")
    line = input()  # 讀取一行字串
    idx_I = line.find("I=")  # 從字串中找 I= 的索引值
    idx_U = line.find("U=")  # 從字串中找 U= 的索引值
    idx_P = line.find("P=")  # 從字串中找 P= 的索引值
    I, U, P = 0, 0, 0  # 電流、電壓、電功率量值
    if idx_I != -1:  # 字串中有 I=
        tmp = ""  # 暫存量值用的字串
        for c in line[idx_I+2:]:  # 從 idx_I+2 開始讀取字元
            if c.isnumeric() or c == '.': tmp += c   # 如果 c 是數字或小數點,c 加到 tmp
            else:  # 如果 c 是其它字元
                I = float(tmp)  # tmp 轉成浮點數存入 I
                I *= prefix[c]  # 乘以前綴詞對應的倍率
                break  # 中止迴圈
    if idx_U != -1:  # 字串中有 U=
        tmp = ""  # 暫存量值用的字串
        for c in line[idx_U+2:]:  # 從 idx_U+2 開始讀取字元
            if c.isnumeric() or c == '.': tmp += c   # 如果 c 是數字或小數點,c 加到 tmp
            else:  # 如果 c 是其它字元
                U = float(tmp)  # tmp 轉成浮點數存入 U
                U *= prefix[c]  # 乘以前綴詞對應的倍率
                break  # 中止迴圈
    if idx_P != -1:  # 字串中有 P=
        tmp = ""  # 暫存量值用的字串
        for c in line[idx_P+2:]:  # 從 idx_P+2 開始讀取字元
            if c.isnumeric() or c == '.': tmp += c   # 如果 c 是數字或小數點,c 加到 tmp
            else:  # 如果 c 是其它字元
                P = float(tmp)  # tmp 轉成浮點數存入 P
                P *= prefix[c]  # 乘以前綴詞對應的倍率
                break  # 中止迴圈
    ### 印出答案 ###
    if idx_I == -1: print(f"I={P/U:.2f}A")  # 求電流
    elif idx_U == -1: print(f"U={P/I:.2f}V")  # 求電壓
    elif idx_P == -1: print(f"P={I*U:.2f}W")  # 求電功率


C++ 程式碼


使用時間約為 1 ms,記憶體約為 336 kB,通過測試。
#include <iostream>
#include <string>
#include <map>
#include <cctype>
#include <iomanip>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    map<char, float> prefix = {{'m', 0.001}, {'k', 1000.0}, {'M', 1000000.0},
                             {'A', 1.0}, {'V', 1.0}, {'W', 1.0}};  // 前綴詞及單位對應的倍率
    int T; cin >> T; cin.ignore();  // T 組測資
    for(int t=1; t<=T; t++) {  // t = 1 ~ T
        if (t > 1) cout << "\n";  // 如果不是第 1 組測資,先換行
        cout << "Problem #" << t << "\n";
        string line; getline(cin, line);  // 讀取一行字串
        // 從字串中找 I=、U=、P= 的索引值
        auto idx_I = line.find("I="), idx_U = line.find("U="), idx_P = line.find("P=");
        float I = 0.0, U = 0.0, P = 0.0;  // 電流、電壓、電功率量值
        int m = (int)line.size();  // 字串長度
        if (idx_I != string::npos) {  // 字串中有 I=
            string tmp = "";  // 暫存量值用的字串
            for(int i=(int)idx_I+2; i<m; i++) {  // 從 idx_I+2 開始讀取字元
                char c = line[i];
                if (isdigit(c) || c == '.') {  // 如果 c 是數字或小數點
                    tmp += c;  // c 加到 tmp
                } else {  // 如果 c 是其它字元
                    I = stof(tmp);  // tmp 轉成浮點數存入 I
                    I *= prefix[c];  // 乘以前綴詞對應的倍率
                    break;  // 中止迴圈
                }
            }
        }
        if (idx_U != string::npos) {  // 字串中有 U=
            string tmp = "";  // 暫存量值用的字串
            for(int i=(int)idx_U+2; i<m; i++) {  // 從 idx_U+2 開始讀取字元
                char c = line[i];
                if (isdigit(c) || c == '.') {  // 如果 c 是數字或小數點
                    tmp += c;  // c 加到 tmp
                } else {  // 如果 c 是其它字元
                    U = stof(tmp);  // tmp 轉成浮點數存入 U
                    U *= prefix[c];  // 乘以前綴詞對應的倍率
                    break;  // 中止迴圈
                }
            }
        }
        if (idx_P != string::npos) {  // 字串中有 P=
            string tmp = "";  // 暫存量值用的字串
            for(int i=(int)idx_P+2; i<m; i++) {  // 從 idx_P+2 開始讀取字元
                char c = line[i];
                if (isdigit(c) || c == '.') {  // 如果 c 是數字或小數點
                    tmp += c;  // c 加到 tmp
                } else {  // 如果 c 是其它字元
                    P = stof(tmp);  // tmp 轉成浮點數存入 P
                    P *= prefix[c];  // 乘以前綴詞對應的倍率
                    break;  // 中止迴圈
                }
            }
        }
        /* 印出答案 */
        if (idx_I == string::npos) {  // 求電流
            cout << fixed << setprecision(2) << "I=" << P/U << "A\n";
        } else if (idx_U == string::npos) {  // 求電壓
            cout << fixed << setprecision(2) << "U=" << P/I << "V\n";
        } else if (idx_P == string::npos) {  // 求電壓
            cout << fixed << setprecision(2) << "P=" << I*U << "W\n";
        }
    }
    return 0;
}


沒有留言:

張貼留言