栈 RE 50分求助
查看原帖
栈 RE 50分求助
408086
StrangeSolver楼主2021/7/28 21:35

代码如下

#include<bits/stdc++.h>
using namespace std;
stack <long long> s1;
stack <char> s2;
string st;
int psz(int i){
	long long ans=0,len=0;
	while(isdigit(st[i])){
		ans=ans*10+st[i]-'0';
		i++;
		len++;
		ans%=10000;
	}
	//cout<<ans<<endl;
	s1.push(ans%10000);
	return len;
}
void js(){
	long long a=s1.top();
	s1.pop();
	long long b=s1.top();
	s1.pop();
	//cout<<a<<" "<<b<<" "<<s2.top()<<endl;
	if(s2.top()=='*') s1.push((a*b)%10000);
	else s1.push((a+b)%10000);
	s2.pop();
}
int main(){
	cin>>st;
	for(long long i=0;i<st.size();i++){
		if(isdigit(st[i])){
			long long len=psz(i);
			i=len+i-1;
		}
		else if(st[i]=='*'){
			while(s2.top()=='*') js();
			s2.push('*');
		}
		else if(st[i]=='+'){
			while(!s2.empty()) js();
			s2.push('+');
		}
	}
	while(!s2.empty()) js();
	cout<<s1.top();
	return 0;
}

感谢大佬们的帮助!

2021/7/28 21:35
加载中...