1 条题解

  • 0
    @ 2025-5-28 16:48:59
    #include <iostream>
    #include <string>
    #include <stack>
    #include <map>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int roman_to_int(const string &s) {
        map<char, int> value;
        value['I'] = 1;
        value['V'] = 5;
        value['X'] = 10;
        value['L'] = 50;
        value['C'] = 100;
        value['D'] = 500;
        value['M'] = 1000;
    
        int result = 0;
        int prev = 0;
        for (int i = s.size() - 1; i >= 0; --i) {
            int curr = value[s[i]];
            if (curr < prev)
                result -= curr;
            else
                result += curr;
            prev = curr;
        }
        return result;
    }
    
    string int_to_roman(int num) {
        vector<pair<int, string> > roman;
        roman.push_back(make_pair(1000, "M"));
        roman.push_back(make_pair(900, "CM"));
        roman.push_back(make_pair(500, "D"));
        roman.push_back(make_pair(400, "CD"));
        roman.push_back(make_pair(100, "C"));
        roman.push_back(make_pair(90, "XC"));
        roman.push_back(make_pair(50, "L"));
        roman.push_back(make_pair(40, "XL"));
        roman.push_back(make_pair(10, "X"));
        roman.push_back(make_pair(9, "IX"));
        roman.push_back(make_pair(5, "V"));
        roman.push_back(make_pair(4, "IV"));
        roman.push_back(make_pair(1, "I"));
    
        string result = "";
        for (int i = 0; i < roman.size(); ++i) {
            while (num >= roman[i].first) {
                result += roman[i].second;
                num -= roman[i].first;
            }
        }
        return result;
    }
    
    int main() {
        string input;
        stack<int> stk;
    
        while (cin >> input) {
            if (input == "+" || input == "-" || input == "*" || input == "/") {
                if (stk.size() < 2) {
                    cout << "stack underflow" << endl;
                    continue;
                }
                int a = stk.top(); stk.pop();
                int b = stk.top(); stk.pop();
    
                if (input == "+") stk.push(b + a);
                else if (input == "-") stk.push(b - a);
                else if (input == "*") stk.push(b * a);
                else if (input == "/") {
                    if (a == 0) {
                        cout << "division by zero exception" << endl;
                        stk.push(b);
                    } else {
                        stk.push(b / a);
                    }
                }
            } else if (input == "=") {
                if (stk.empty()) {
                    cout << "stack underflow" << endl;
                } else {
                    int val = stk.top();
                    if (val <= 0 || val > 4999) {
                        cout << "out of range exception" << endl;
                    } else {
                        cout << int_to_roman(val) << endl;
                    }
                }
            } else {
                stk.push(roman_to_int(input));
            }
        }
    
        return 0;
    }
    • 1

    信息

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