有关栈的一些问题,给关
查看原帖
有关栈的一些问题,给关
739307
All_Failed_OI_Cici楼主2024/8/2 20:35

P1449求调 (有什么问题呢?)

#include<bits/stdc++.h>
using namespace std;
stack<int> sta;
string s,str;
int change(string s1){
	int ans=0;
	for(int i=0;i<s.size();i++){
		ans=ans*10;
		ans+=s[i]-'0';
	}
	return ans;
}
signed main(){
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]=='@') break;
		if(s[i]=='.'){
			sta.push(change(str));
			str="";
			continue;
		}
		if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){
			int x=sta.top();
			sta.pop();
			int y=sta.top();
			sta.pop();
			if(s[i]=='+') sta.push(x+y);
			if(s[i]=='-') sta.push(x-y);
			if(s[i]=='*') sta.push(x*y);
			if(s[i]=='/') sta.push(x/y);
		}
		else str.push_back(s[i]);
//		cout<<sta.top()<<endl;
	}
	cout<<sta.top();
	return 0;
}

备注:没有提交。如果去掉注释的那行,会return一个奇怪的值;如上的代码会输出0。只试了样例1。

2024/8/2 20:35
加载中...