为什么在#9TLE了
查看原帖
为什么在#9TLE了
1074084
asd890123楼主2024/8/27 11:41

个人认为是死循环的锅,到底是为什么

#include <iostream>
#include <sstream>
#include <string>

const int INF = 1000000000;

int st[2005];
std::string temp,message = "";

int main(){

    std::cin.tie(0)->sync_with_stdio(0);

    int n;

    while (std::getline(std::cin,temp),temp.substr(0,3) != "END") message += temp,message += '\n';
    for (std::cin >> n;n--;){

        int x,top = 0;
        std::stringstream scin(message);

        std::cin >> x;
        st[++top] = x;
        while (scin >> temp){

            if (temp == "NUM") scin >> x,st[++top] = x;
            else if (temp == "POP") {if (top) --top;else goto Error;}
            else if (temp == "INV") {if (top) st[top] *= -1;else goto Error;}
            else if (temp == "DUP") {

                if (top){int tmp = st[top];st[++top] = tmp;}
                else goto Error;

            }
            else if (temp == "SWP") {if (top < 2) goto Error;std::swap(st[top],st[top - 1]);}
            else if (temp == "ADD") {
                
                if (top < 2) goto Error;
                int tmp = st[top--] + st[top--];
                if (std::abs(tmp) > INF) goto Error;
                st[++top] = tmp;
                
            }
            else if (temp == "SUB") {
                
                if (top < 2) goto Error;
                int x = st[top--],y = st[top--],tmp = y - x;
                if (std::abs(tmp) > INF) goto Error;
                st[++top] = tmp;
                
            }
            else if (temp == "MUL") {
                
                if (top < 2) goto Error;
                long long tmp = 1LL * st[top--] * st[top--];
                if (std::abs(tmp) > INF) goto Error;
                st[++top] = (int)tmp;
                
            }
            else if (temp == "DIV") {                

                if (top < 2 || st[top] == 0) goto Error;
                int x = st[top--],y = st[top--],tmp = y / x;
                st[++top] = tmp;
                
            }
            else if (temp == "MOD") {                

                if (top < 2 || st[top] == 0) goto Error;
                int x = st[top--],y = st[top--],tmp = y % x;
                st[++top] = tmp;
                
            }

        }
        if (top != 1) goto Error;
        std::cout << st[top] << '\n';
        continue;
        Error:std::cout << "ERROR\n";

    }

    return 0;
}
2024/8/27 11:41
加载中...