社区讨论
求助
P1449后缀表达式参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @lo2q4nbu
- 此快照首次捕获于
- 2023/10/23 17:57 2 年前
- 此快照最后确认于
- 2023/10/23 17:57 2 年前
为啥没法输出啊,而且时间复杂度高的快要炸了
代码是参考洛谷的深入浅出上15-5内容上的算法写的
声明:没有照搬书上的代码,是借鉴后参考着自己手写的代码,算法是参照书上的,但代码绝对自己写的
CPP#include<bits/stdc++.h>
using namespace std;
int main(){
int s,a,b;
stack<int> n;//
char text;
do{
text=getchar();//每次只读入一个
if(text>='0'||text<='9'){
s=s*10+text-'0';
}
else
if(text='.'){
n.push(s);
s=0;
}
else if(text!='@'){
a=n.top();
n.pop();
b=n.top();//此时栈中前两个元素待运算
if(text=='+'){
n.push(a+b);//压入
}
else
if(text=='-'){
n.push(b-a);
}
else
if(text=='*'){
n.push(a*b);
}
else
if(text=='/'){
n.push(a/b);
}
}
}while(text!='@');
printf("%d/n",n.top());
return 0;
}
希望dalao能解答疑问,初次接触数据结构
回复
共 2 条回复,欢迎继续交流。
正在加载回复...