求救!
查看原帖
求救!
112631
Lovable_Wind楼主2021/8/31 23:00
#include<bits/stdc++.h>
using namespace std;
const double pi=3.14;
const int inf=0x3f3f3f3f;
const int NIL=-1;
int n;
bool flag=0;//flag=1;
stack<long long> S;
int read()
{
    int ans=0,flag=1;
    char ch=getchar();
    while( (ch>'9' || ch<'0') && ch!='-' ) ch=getchar();
    if(ch=='-') flag=-1,ch=getchar();
    while(ch>='0' && ch<='9') ans=ans*10+ch-'0',ch=getchar();
    return ans*flag;
}
struct Node{
	string s;
	long long c;
}d[100010];
bool NUM(long long x){
	if (abs(x)>1e9){
		flag=1;
		return 0;
	}
	S.push(x);
	return 1;
	
}
bool POP(){
	if (S.empty()){
		flag=1;
		return 0;
	}
	S.pop();
	return 1;
}
bool INV(){
	if (S.empty()){
		flag=1;
		return 0;
	}
	long long temp=S.top();
	S.pop();
	if (!NUM((0-temp))){
		flag=1;
		return 0;
	}
	else return 1;
}
bool DUP(){
	if (S.empty()){
		flag=1;
		return 0;
	}
	if (!NUM(S.top())){
	flag=1;return 0;
	}
	else return 1;
}
bool SWP(){
	if (S.size()<2){
		return 0;
	}
	long long tmp1=S.top(),tmp2;
	S.pop();
	tmp2=S.top();
	S.pop();
	S.push(tmp2);
	S.push(tmp1);
	return 1;
}
bool ADD(){
	if (S.size()<2){
		flag=1;
		return 0;
	}
	long long tmp1=S.top(),tmp2;
	S.pop();
	tmp2=S.top();
	S.pop();
	if (abs(tmp1+tmp2)>1e9){
		flag=1;return 0;
	}
	else S.push(tmp1+tmp2);
	return 1;
}
bool SUB(){
	if (S.size()<2){
		flag=1;
		return 0;
	}
	long long tmp1=S.top(),tmp2;
	S.pop();
	tmp2=S.top();
	S.pop();
	if (abs(tmp2-tmp1)>1e9){
		flag=1;
		return 0;
	}
	else S.push(tmp2-tmp1);
	return 1;
}
bool MUL(){
	if (S.size()<2){
		flag=1;
		return 0;
	}
	long long tmp1=S.top(),tmp2;
	S.pop();
	tmp2=S.top();
	if (abs(tmp2*tmp1)>1e9){
		flag=1;return 0;
	}
	else S.push(tmp2*tmp1);
	return 1;
}
bool DIV(){
	if (S.size()<2){
		flag=1;
		return 0;
	}
	long long tmp1=S.top(),tmp2;
	S.pop();
	tmp2=S.top();
	S.pop();
	if (tmp1==0){
		flag=1;
		return 0;
	}
	if (abs(tmp2/tmp1)>1e9){
		flag=1;return 0;
	}
	else S.push(tmp2/tmp1);
	return 1;
}
bool MOD(){
	if (S.size()<2){
		flag=1;
		return 0;
	}
	long long tmp1=S.top(),tmp2;
	S.pop();
	tmp2=S.top();
	S.pop();
	if (tmp1==0){
		flag=1;return 0;
	}
	if (abs(tmp2%tmp1)>1e9){
		flag=1;
		return 0;
	}
	else S.push(tmp2%tmp1);
	return 1;
}
void solve(int cnt){
	long long T,x;
	T=read();
	while(T--){
		flag=0;
		while(!S.empty()) S.pop();
		cin>>x;
		if (!NUM(x)){cout<<"ERROR"<<endl;flag=1;continue;}
		for (int i=1;i<=cnt;i++){
			if (d[i].s=="NUM"){
				if (NUM(d[i].c)) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="POP"){
				if (POP()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="INV"){
				if (INV()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="DUP"){
				if (DUP()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="SWP"){
				if (SWP()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="ADD"){
				if (ADD()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="SUB"){
				if (SUB()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="DIV"){
				if (DIV()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}else if (d[i].s=="MOD"){
				if (MOD()) continue;
				else {cout<<"ERROR"<<endl;flag=1;continue;}
			}
		}
		if (S.size()==1&&flag==0){
			cout<<S.top()<<endl;
			S.pop();
		}else{
			if (flag==0)
				cout<<"ERROR"<<endl;
			while(!S.empty()) S.pop();
		}
	}
}
void setup(){
	int cnt=0;
	string c;
	while(cin>>c)
    {
        if(c=="END") break;
        cnt++;
        d[cnt].s=c;
        if(c=="NUM")
        {
            long long x;
            cin>>x;
            d[cnt].c=x;
        }
    }
    solve(cnt);
}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	setup();
}
 

已证实,错误是在"DIV"和"MOD"后因不明原因判错输出ERROR致使,还有在第二个点显示"Too long",原因不明,求助!

2021/8/31 23:00
加载中...