社区讨论

求助 wa三组

P1449后缀表达式参与者 1已保存回复 0

讨论操作

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

当前回复
0 条
当前快照
1 份
快照标识符
@mi7vy9e7
此快照首次捕获于
2025/11/21 04:30
4 个月前
此快照最后确认于
2025/11/21 04:30
4 个月前
查看原帖
莫名wa x是个用来算数的队列 q是计算的栈
CPP
#include<iostream>
#include<stack>
#include<cstring>
#include<queue>
using namespace std;
int t1,t2,cnt,tot;
string s;
stack <int > q;
queue <int > x;
int main() {
	cin>>s;
	int len=s.length();
	for(int i=0; i<len; i++) {
		if(s[i]=='@')
			break;
		if(s[i]<='9'&&s[i]>='0') {
			x.push(s[i]-'0');
			cnt++;
			continue;
		}
		if(s[i]=='.') {
			tot=0;
			while(cnt--) {
				if(cnt==0) {
					tot+=x.front();
					x.pop();
				} else {
					tot+=x.front()*cnt*10;
					x.pop();
				}
			}
			q.push(tot);
			cnt=0;
		} else if(s[i]!='.') {
			t1=q.top();
			q.pop();
			t2=q.top();
			q.pop();
			if(s[i]=='+') {
				q.push(t1+t2);
			}
			if(s[i]=='-') {
				q.push(t2-t1);
			}
			if(s[i]=='*') {
				q.push(t1*t2);
			}
			if(s[i]=='/') {
				q.push(t2/t1);
			}
		}
	}
	cout<<q.top();
	return 0;
}

回复

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

正在加载回复...