救救孩子,调了 1145141919810 遍了还是 90pts #4 WA。
查看原帖
救救孩子,调了 1145141919810 遍了还是 90pts #4 WA。
988735
THE_DARK_FOREST楼主2023/8/9 18:23
#include <stack>
#include <iostream>
#include <cmath>
#include <cstring>
#define int long long
#define there_no_st_empty if(st.empty()){RIP();return;/*It's a excellent operate, but there is no any elements in this stack!!!*/}
using namespace std;

string command[114514];
int nums[114514];
int cnt = 1;
stack<int> st;

inline bool check(int x)
{
    return abs(x) > 1e9;
}

inline void RIP()
{
    cout << "ERROR\n";
}

inline void this_is_the_really_main_function(int n)
{
    while (st.size())
    {
        st.pop();
    }
    st.push(n);
    if (abs(n) > 1e9)
    {
        RIP();
        return;
    }
    for (int i = 0; i <= cnt + 1; i++)
    {
        if (abs(st.top()) > 1e9)
        {
            RIP(); // R.I.P f(x)
            return;
        }
        if (command[i] == "NUM")
        {
            if (check(nums[i]))
            {
                RIP();
                return;
            }
            st.push(nums[i]);
        }
        if (command[i] == "POP")
        {
            there_no_st_empty;
            st.pop();
        }
        if (command[i] == "INV")
        {
            there_no_st_empty;
            int tmp = st.top();
            st.pop();
            st.push(-tmp);
        }
        if (command[i] == "DUP")
        {
            there_no_st_empty;
            st.push(st.top());
        }
        if (command[i] == "SWP")
        {
            if (st.size() < 2)
            {
                RIP(); /*R.I.P*/
                return;
            }
            int top1 = st.top();
            st.pop();
            int top2 = st.top();
            st.pop();
            st.push(top2);
            st.push(top1);
        }
        if (command[i] == "ADD")
        {
            if (st.size() < 2)
            {
                RIP();
                return;
            }
            int sum = st.top();
            st.pop();
            sum += st.top();
            if (check(sum))
            {
                RIP();
                return;
            }
            st.pop();
            st.push(sum);
        }
        if (command[i] == "SUB")
        {
            if (st.size() < 2)
            {
                RIP();
                return;
            }
            int sub = st.top();
            st.pop();
            sub = st.top() - sub;  
            st.pop();
            if (check(sub))
            {
                RIP();
                return;
            }
            st.push(sub);
        }
        if (command[i] == "MUL")
        {
            if (st.size() < 2)
            {
                RIP();
                return;
            }
            int mul = st.top();
            st.pop();
            mul *= st.top();
            if (check(mul))
            {
                RIP();
                return;
            }
            st.pop();
            st.push(mul);
        }
        if (command[i] == "DIV")
        {
            if (st.size() < 2)
            {
                RIP();
                return;
            }
            int div = st.top();
            if (!div)
            {
                RIP(); // Holy shit, It will be runtime error!
                return;
            }
            st.pop();
            div = st.top() / div;
            if (check(div))
            {
                RIP();
                return;
            }
            st.pop();
            st.push(div);
        }
        if (command[i] == "MOD")
        {
            if (st.size() < 2)
            {
                RIP();
                return;
            }
            int mod = st.top();
            if (!mod)
            {
                RIP(); // Why do I runtime error ? Why !!
                return;
            }
            st.pop();
            mod = st.top() % mod;
            if (check(mod))
            {
                RIP();
                return;
            }
            st.pop();
            st.push(mod);
        }
    }   
    if (st.size() != 1)
    {
        // What Elements in this stack?
        RIP();
        return;
    }
    
    cout << st.top() << endl;
    
}
signed main()
{
    string str;
    int n;
    for (; ;cnt++)
    {
        cin >> command[cnt];
        if (command[cnt] == "NUM")
        {
            cin >> nums[cnt];
        }
        if (command[cnt] == "END")
        {
            break;
        }
    }
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int k;
        cin >> k;
        this_is_the_really_main_function(k);
    }
}

都快和题解一模一样了 QwQ。

2023/8/9 18:23
加载中...