超时求调玄关(死循环)
  • 板块学术版
  • 楼主_TAT_
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/4/22 19:26
  • 上次更新2024/4/22 21:42:53
查看原帖
超时求调玄关(死循环)
1073331
_TAT_楼主2024/4/22 19:26

题

#include <bits/stdc++.h>
using namespace std;
stack<int> k;
stack<int> t;
string s;
void ca(){
    int y=t.top();
    t.pop();
    int x=t.top();
    t.pop();
    if(k.top()=='+') t.push(x+y);
    if(k.top()=='-') t.push(x-y);
    if(k.top()=='*') t.push(x*y);
    if(k.top()=='/') t.push(x/y);
    else{
        int cnt=1;
        for(int i=1;i<=y;i++){
            cnt*=x;
        }
        t.push(cnt);
    }
}
int qwq(char p){
    if(p=='+'||p=='-') return 1;
    else if(p=='*'||p=='/') return 2;
    else if(p=='^') return 3;
    else return 0;
}
int main(){
    int sum=0;
    k.push(0);
    cin>>s;
    s[s.size()]=')';
    int l=s.size();
    for(int i=0;i<l;i++){
        if(s[i]<='9'&&s[i]>='0'){
            sum=sum*10+(s[i]-'0');
        }
        else{
            if(s[i]=='('){
                k.push(0);
            }
            else if(s[i]==')'){
                while(k.top()!='('){
                    ca();
                }
            }
            else{
                int num=qwq(s[i]);
                if(num<=k.top()){
                    while(num<=k.top()){
                        ca();
                    }
                }
                k.push(num);
            }
            t.push(sum);
            sum=0;
        }
    }
	return 0;
}

玄关玄关

2024/4/22 19:26
加载中...