1 条题解

  • 0
    @ 2026-4-1 16:32:06
    #include <bits/stdc++.h>
    using namespace std;
    
    int precedence(char op) {
        if (op == '+' || op == '-') return 1;
        if (op == '*' || op == '/') return 2;
        return 0;
    }
    
    int main() {
        ios::sync_with_stdio(false);cin.tie(nullptr);
        string s; cin >> s;
        vector<string> output;
        vector<char> st;
        for (int i = 0; i < s.size(); ) {
            if (isdigit((unsigned char)s[i])) {
                int j = i;
                while (j < s.size() && isdigit((unsigned char)s[j])) ++j;
                output.emplace_back(s.substr(i, j - i));
                i = j;
                continue;
            }
            char c = s[i];
            if (c == '(')
                st.push_back(c);
            else if (c == ')') {
                while (!st.empty() && st.back() != '(') output.emplace_back(string(1, st.back())), st.pop_back();
                if (!st.empty() && st.back() == '(') st.pop_back();
            } else {
                while (!st.empty() && st.back() != '(' &&
                       precedence(st.back()) >= precedence(c)) {
                    output.emplace_back(string(1, st.back()));
                    st.pop_back();
                }
                st.push_back(c);
            }
            ++i;
        }
        while (!st.empty()) output.emplace_back(string(1, st.back())), st.pop_back();
        for (int i = 0; i < (int)output.size(); i ++) {
            if(i) cout << ' ';
            cout << output[i];
        }
        cout << '\n';
        return 0;
    }
    
    • 1

    信息

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