1 条题解

  • 0
    @ 2025-5-6 20:00:12

    解决思路

    1. 输入处理:读取输入的测试用例数量,然后逐个处理每个测试用例。
    2. 提取数据字段:对于每个测试用例的字符串,我们需要找到两个数据字段。数据字段的格式是固定的,可以通过查找=和单位(AVW)来定位。
    3. 解析数据字段:对于每个数据字段,提取其概念(P、U、I)、数值和前缀,并将数值转换为标准单位(无前缀)。
    4. 计算缺失的量:根据已知的两个量,计算第三个量。
    5. 输出结果:按照要求的格式输出结果,注意数值的格式化和单位的正确性。

    解决代码

    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <sstream>
    #include <vector>
    #include <cctype>
    #include <cstdlib> // for atof
    
    using namespace std;
    
    struct DataField {
        char concept;
        double value;
        char prefix;
        char unit;
    };
    
    DataField parseDataField(const string& field) {
        DataField data;
        size_t eq_pos = field.find('=');
        data.concept = field[eq_pos - 1];
        
        size_t unit_pos = field.find_first_of("AVW", eq_pos);
        data.unit = field[unit_pos];
        
        string num_str = field.substr(eq_pos + 1, unit_pos - eq_pos - 1);
        data.prefix = '\0';
        if (!num_str.empty() && (num_str[num_str.size()-1] == 'm' || num_str[num_str.size()-1] == 'k' || num_str[num_str.size()-1] == 'M')) {
            data.prefix = num_str[num_str.size()-1];
            num_str = num_str.substr(0, num_str.size() - 1);
        }
        // Replace stod with atof (C-style conversion)
        data.value = atof(num_str.c_str());
        
        return data;
    }
    
    double getValueWithPrefix(double value, char prefix) {
        switch (prefix) {
            case 'm': return value * 0.001;
            case 'k': return value * 1000;
            case 'M': return value * 1000000;
            default: return value;
        }
    }
    
    void solve() {
        int T;
        cin >> T;
        cin.ignore(); // Ignore the newline after T
        
        for (int k = 1; k <= T; ++k) {
            string line;
            getline(cin, line);
            
            vector<DataField> fields;
            size_t pos = 0;
            while (pos < line.size()) {
                size_t eq_pos = line.find('=', pos);
                if (eq_pos == string::npos) break;
                
                // Find the start of the field (start of concept)
                size_t start = eq_pos - 1;
                while (start > 0 && !isspace(line[start - 1])) {
                    --start;
                }
                
                // Find the end of the field (end of unit)
                size_t unit_pos = line.find_first_of("AVW", eq_pos);
                if (unit_pos == string::npos) break;
                size_t end = unit_pos + 1;
                
                string field_str = line.substr(start, end - start);
                DataField field = parseDataField(field_str);
                fields.push_back(field);
                
                pos = end;
            }
            
            double P = -1, U = -1, I = -1;
            for (vector<DataField>::const_iterator it = fields.begin(); it != fields.end(); ++it) {
                const DataField& field = *it;
                double value = getValueWithPrefix(field.value, field.prefix);
                if (field.concept == 'P') {
                    P = value;
                } else if (field.concept == 'U') {
                    U = value;
                } else if (field.concept == 'I') {
                    I = value;
                }
            }
            
            cout << "Problem #" << k << endl;
            if (P == -1) {
                P = U * I;
                cout << fixed << setprecision(2) << "P=" << P << "W" << endl;
            } else if (U == -1) {
                U = P / I;
                cout << fixed << setprecision(2) << "U=" << U << "V" << endl;
            } else if (I == -1) {
                I = P / U;
                cout << fixed << setprecision(2) << "I=" << I << "A" << endl;
            }
            cout << endl;
        }
    }
    
    int main() {
        solve();
        return 0;
    }
    
    • 1

    信息

    ID
    1257
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者