WA #9 求条(玄关)
查看原帖
WA #9 求条(玄关)
890515
ikunTLE楼主2024/11/1 21:03

屎山代码求条,小号玄 \infty 关。

#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=2e3+10,INF=1e9;
const long long ERR=9e18;
char op[N][5];
int num[N],m;
stack<long long>st;
long long f(int x){
	while(!st.empty())
		st.pop();
	st.push(x);
	for(int i=1;i<=m;++i){	
		if(!strcmp(op[i],"NUM"))
			st.push(num[i]);
		else if(!strcmp(op[i],"POP")){
			if(st.empty())
				return ERR;
			st.pop();
		}
		else if(!strcmp(op[i],"INV")){
			if(st.empty())
				return ERR;
			int x=st.top();
			st.pop();
			st.push(-x);
		}
		else if(!strcmp(op[i],"DUP")){
			if(st.empty())
				return ERR;
			st.push(st.top());
		}
		else if(!strcmp(op[i],"SWP")){
			if(st.size()<2)
				return ERR;
			long long x=st.top();
			st.pop();
			long long y=st.top();
			st.pop();
			st.push(x);
			st.push(y);
		}
		else if(!strcmp(op[i],"ADD")){
			if(st.size()<2)
				return ERR;
			long long x=st.top();
			st.pop();
			long long y=st.top();
			st.pop();
			if(llabs(y+x)>INF)
				return ERR;
			st.push(y+x);
		}
		else if(!strcmp(op[i],"SUB")){
			if(st.size()<2)
				return ERR;
			long long x=st.top();
			st.pop();
			long long y=st.top();
			st.pop();
			if(llabs(y+x)>INF)
				return ERR;
			st.push(y-x);
		}
		else if(!strcmp(op[i],"MUL")){
			if(st.size()<2)
				return ERR;
			long long x=st.top();
			st.pop();
			long long y=st.top();
			st.pop();
			if(llabs(y+x)>INF)
				return ERR;
			st.push(y*x);
		}
		else if(!strcmp(op[i],"DIV")){
			if(st.size()<2)
				return ERR;
			long long x=st.top();
			st.pop();
			long long y=st.top();
			st.pop();
			if(!x)
				return ERR;
			st.push(y/x);
		}
		else if(!strcmp(op[i],"MOD")){
			if(st.size()<2)
				return ERR;
			long long x=st.top();
			st.pop();
			long long y=st.top();
			st.pop();
			if(!x)
				return ERR;
			st.push(y%x);
		}
		else return st.top();
	}
	return st.empty()?st.top():ERR;
}
int main(){
	while(++m){
		scanf("%s",op[m]);
		if(!strcmp(op[m],"NUM"))
			num[m]=read();
		if(!strcmp(op[m],"END"))
			break;
	}
	int n=read();
	while(n--){
		long long x=f(read());
		if(x==ERR)
			printf("ERROR\n");
		else printf("%d\n",x);
	}
	return 0;
}
2024/11/1 21:03
加载中...