0分TLE求调
查看原帖
0分TLE求调
392197
1wrcwrc楼主2025/7/25 10:30

附上丑陋代码()

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int n,x,a[2010],t=1,c[2010],f;
bool flag=1;
string s,r="   ";
int qs(string p){
	int m=0,o=p.length();
	for(int j=4;j<o;j++){
		m=m*10+p[j]-48;
	}
	return m;
}
int main(){
	while(s!="END"){
		f++;
		getline(cin,s);
		r[0]=s[0];
		r[1]=s[1];
		r[2]=s[2];
		if(r=="NUM"){
			c[f]=1000000000+qs(s);
		}else if(r=="POP"){
			c[f]=2;
		}else if(r=="INV"){
			c[f]=3;
		}else if(r=="DUP"){
			c[f]=4;
		}else if(r=="SWP"){
			c[f]=5;
		}else if(r=="ADD"){
			c[f]=6;
		}else if(r=="SUB"){
			c[f]=7;
		}else if(r=="MUL"){
			c[f]=8;
		}else if(r=="DIV"){
			c[f]=9;
		}else if(r=="MOD"){
			c[f]=10;
		}else if(r=="END"){
			c[f]=11;
		}
	}
	cin>>n;
	while(n--){
		for(int i=1;i<=t;i++){
			a[i]=0;
		}
		t=1;
		cin>>x;
		a[1]=x;
		flag=1;
		for(int i=1;i<=f;i++){
			if(c[i]>=1000000000){
				t++;
				a[t]=c[i]-1000000000;
			}else if(c[i]==2){//r=="POP"
				if(t==0){
					flag=0;
					break;
				}
				a[t]=0;
				t--;
			}else if(c[i]==3){//r=="INV"
				if(t==0){
					flag=0;
					break;
				}
				a[t]=-a[t];
			}else if(c[i]==4){//r=="DUP"
				if(t==0){
					flag=0;
					break;
				}
				t++;
				a[t]=a[t-1];
			}else if(c[i]==5){//r=="SWP"
				if(t==0||t==1){
					flag=0;
					break;
				}
				if(t>1)swap(a[t],a[t-1]);
				else{
					flag=0;
					break;
				} 
			}else if(c[i]==6){//r=="ADD"
				if(t==0||t==1){
					flag=0;
					break;
				}
				if(a[t]+a[t-1]<=1000000000&&a[t]+a[t-1]>=-1000000000){
					t--;
					a[t]+=a[t+1];
					a[t+1]=0;
				}else{
					flag=0;
					break;
				}
			}else if(c[i]==7){//r=="SUB"
				if(t==0||t==1){
					flag=0;
					break;
				}
				if(a[t-1]-a[t]<=1000000000&&a[t-1]-a[t]>=-1000000000){
					t--;
					a[t]-=a[t+1];
					a[t+1]=0;
				}else{
					flag=0;
					break;
				}
			}else if(c[i]==8){//r=="MUL"
				if(t==0||t==1){
					flag=0;
					break;
				}
				long long e=(long long)a[t]*(long long)a[t-1];
				if(e<=1000000000&&e>=-1000000000){
					t--;
					a[t+1]=0;
					a[t]=e;
				}else{
					flag=0;
					break;
				}
			}else if(c[i]==9){//r=="DIV"
				if(t==0||t==1){
					flag=0;
					break;
				}
				if(a[t]!=0&&a[t-1]/a[t]>=-1000000000&&a[t-1]/a[t]<=1000000000){
					t--;
					a[t]/=a[t+1];
					a[t+1]=0;
				}else{
					flag=0;
					break;
				}
			}else if(c[i]==10){//r=="MOD"
				if(t==0||t==1){
					flag=0;
					break;
				}
				if(a[t]!=0){
					t--;
					a[t]%=a[t+1];
				}else{
					flag=0;
					break;
				}
			}else if(c[i]==11){//r=="END"
				break;
			}
		}
		if(t==1&&flag){
			cout<<a[1];
		}else{
			cout<<"ERROR";
		}
		cout<<endl;
	}
	return 0;
}

2025/7/25 10:30
加载中...