社区讨论

40pts求调,WA on #1~3、5~7,调了关,qwq

P2186小 Z 的栈函数参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mhj32t8t
此快照首次捕获于
2025/11/03 19:55
4 个月前
此快照最后确认于
2025/11/03 19:55
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cnt=1,q,x;
struct node{
	string op;
	ll x;
}a[2005];
stack <ll> st;
bool bigger(ll x){
	return (x<0?-x:x)>=1000000000;
} 
int main(){

	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	for(int i=1;;i++){
		string s;
		cin>>s;
		if(s=="END"){
			break;
		}else{
			cnt++;
			a[i].op=s;
			if (s=="NUM") cin>>a[i].x;
		}
	}
	cin>>q;
	while (q--){
		cin>>x;
		while(!st.empty()) st.pop();
		st.push(x);
		if (bigger(x)){
			cout<<"ERROR\n";
			continue;
		}
		bool fl=0;
		for(int i=1;i<cnt;i++){
			if (a[i].op=="NUM"){
				if (bigger(a[i].x)){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					st.push(x);
				}
				
				
			}else if(a[i].op=="POP"){
				if (st.empty()){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					st.pop();
				}
				
				
			}else if(a[i].op=="INV"){
				if (st.empty()){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					p=-p;
					st.pop();
					st.push(p);
				}
				
				
			}else if(a[i].op=="DUP"){
				if (st.empty()){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					st.push(p);
				}
				
				
			}else if(a[i].op=="SWP"){
				if (st.size()<2){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					st.pop();
					ll pp=st.top();
					st.pop();
					st.push(p);
					st.push(pp);
				}
				
				
			}else if(a[i].op=="ADD"){
				if (st.size()<2){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					st.pop();
					ll pp=st.top();
					st.pop();
					if (bigger(pp+p)){
						fl=1;
						cout<<"ERROR\n";
						break;
					}
					else st.push(pp+p);
				}
				
				
			}else if(a[i].op=="SUB"){
				if (st.size()<2){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					st.pop();
					ll pp=st.top();
					st.pop();
					if (bigger(pp-p)){
						fl=1;
						cout<<"ERROR\n";
						break;
					}
					else st.push(pp-p);
				}
				
				
			}else if(a[i].op=="MUL"){
				if (st.size()<2){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					st.pop();
					ll pp=st.top();
					st.pop();
					if (bigger(pp*p)){
						fl=1;
						cout<<"ERROR\n";
						break;
					}
					else st.push(pp*p);
				}
				
				
			}else if(a[i].op=="DIV"){
				if (st.size()<2){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					st.pop();
					ll pp=st.top();
					st.pop();
					if (p==0 || bigger(pp/p)){
						fl=1;
						cout<<"ERROR\n";
						break;
					}
					else st.push(pp/p);
				}
				
				
			}else if(a[i].op=="MOD"){
				if (st.size()<2){
					fl=1;
					cout<<"ERROR\n";
					break;
				}else{
					ll p=st.top();
					st.pop();
					ll pp=st.top();
					st.pop();
					if (p==0 || bigger(pp%p)){
						fl=1;
						cout<<"ERROR\n";
						break;
					}
					else st.push(pp%p);
				}
			}
		}
		if (fl) continue;
		if (st.size()!=1){
			cout<<"ERROR\n";
			continue;
		}else{
			cout<<st.top()<<"\n";
		}
	}
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...