1 条题解

  • 0
    @ 2025-5-28 0:46:52

    题意分析

    这段代码是一个数据处理程序,主要用于处理商品记录并生成格式化的输出。具体来说:

    1. 输入数据:程序从标准输入读取CSV格式的记录,每行包含多个字段,至少需要3个字段(款式代码、款式名称、描述)
    2. 数据处理
      • 填充款式代码到3位长度
      • 处理缺失的款式名称(从已存在的记录中查找)
      • 构建商品ID和商品描述
      • 格式化价格为两位小数
    3. 输出结果:将处理后的数据以CSV格式输出,包含Item Id、Item Desc和Item Price三个字段

    解题思路

    1. 数据结构设计

      • 使用map<string, string>存储款式代码到款式名称的映射
      • 使用vector<vector<string>>存储所有记录
    2. 数据读取与预处理

      • 逐行读取输入数据,使用split函数按逗号分割字段
      • 提取关键信息(款式代码、款式名称、描述等)
      • 对每条记录进行验证,丢弃无效记录
    3. 数据处理逻辑

      • 补全款式代码至3位
      • 为缺失款式名称的记录查找对应的款式名称
      • 构建商品ID和商品描述,控制长度
      • 将价格转换为标准格式
    4. 输出处理

      • 按指定格式输出处理后的结果
      • 丢弃不符合条件的记录(如Item Id过长)
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cstdio>
    #include <cstdlib>
    #include <iomanip>
    #include <map>
    
    using namespace std;
    
    vector<string> split(const string &s, char delim) {
        vector<string> elems;
        string item;
        for (size_t i = 0; i < s.size(); ++i) {
            if (s[i] == delim) {
                if (!item.empty()) {
                    elems.push_back(item);
                    item.clear();
                }
            } else {
                item += s[i];
            }
        }
        if (!item.empty()) {
            elems.push_back(item);
        }
        return elems;
    }
    
    string pad_style_code(const string &code) {
        if (code.size() >= 3) return code;
        return string(3 - code.size(), '0') + code;
    }
    
    string build_item_desc(const string &style_name, const string &extension) {
        if (extension.empty()) return style_name;
        string desc = style_name + "-" + extension;
        if (desc.size() > 30) {
            desc = desc.substr(0, 30);
        }
        return desc;
    }
    
    string format_price(const string &price_str) {
        if (price_str.empty()) return "0.00";
        int price = atoi(price_str.c_str());
        char buf[16];
        sprintf(buf, "%.2f", price / 100.0);
        return string(buf);
    }
    
    int main() {
        map<string, string> style_name_map;
        vector<vector<string> > records;
        string line;
        
        // 读取所有记录
        while (getline(cin, line)) {
            if (line.empty()) continue; // 跳过空行
            
            vector<string> fields = split(line, ',');
            if (fields.size() < 3) continue; // 无效记录
            
            string style_code = fields[0];
            string style_name = fields.size() > 1 ? fields[1] : "";
            string description = fields[2];
            string extension = fields.size() > 3 ? fields[3] : "";
            string unit_price = fields.size() > 4 ? fields[4] : "";
            
            // 存储第一条记录的Style Name
            if (!style_name.empty() && style_name_map.find(style_code) == style_name_map.end()) {
                style_name_map[style_code] = style_name;
            }
            
            vector<string> record;
            record.push_back(style_code);
            record.push_back(style_name);
            record.push_back(description);
            record.push_back(extension);
            record.push_back(unit_price);
            records.push_back(record);
        }
        
        // 输出标题
        cout << "Item Id,Item Desc,Item Price" << endl;
        
        // 处理每条记录
        for (size_t i = 0; i < records.size(); ++i) {
            string style_code = records[i][0];
            string style_name = records[i][1];
            string description = records[i][2];
            string extension = records[i][3];
            string unit_price = records[i][4];
            
            // 处理缺失的Style Name
            if (style_name.empty()) {
                if (style_name_map.find(style_code) != style_name_map.end()) {
                    style_name = style_name_map[style_code];
                } else {
                    continue; // 丢弃没有Style Name的记录
                }
            }
            
            // 构建Item Id
            string padded_code = pad_style_code(style_code);
            string item_id = padded_code + description;
            if (item_id.size() > 13) continue; // 丢弃过长的Item Id
            
            // 构建Item Desc
            string item_desc = build_item_desc(style_name, extension);
            
            // 格式化价格
            string item_price = format_price(unit_price);
            
            // 输出结果
            cout << item_id << "," << item_desc << "," << item_price << endl;
        }
        
        return 0;
    }
    ```cpp
    • 1

    信息

    ID
    491
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者