蒟蒻的来做梦7.20总结
应该没人写了笔记吧但还是放一个黑板
U445194 栈进阶--3 后缀表达式求值
#include<iostream>
#include<stack>
typedef long long ll;
using namespace std;
stack<int>s;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
{
char c;
while(cin.peek()!='@'){
switch(cin.peek()){
case ' ':{
cin.get();
break;
}
case '+':{
cin>>c;
ll tp=s.top();
s.pop();
ll tp2=s.top();
s.pop();
s.push(tp2+tp);
break;
}
case '-':{
cin>>c;
ll tp=s.top();
s.pop();
ll tp2=s.top();
s.pop();
s.push(tp2-tp);
break;
}
case '*':{
cin>>c;
ll tp=s.top();
s.pop();
ll tp2=s.top();
s.pop();
s.push(tp2*tp);
break;
}
case '/':{
cin>>c;
ll tp=s.top();
s.pop();
ll tp2=s.top();
s.pop();
s.push(tp2/tp);
break;
}
default:{
ll i;
cin>>i;
s.push(i);
}
}
}
}
cout<<s.top();
return 0;
}
U445233 栈进阶--5 表达式求值
#include<iostream>
#include<stack>
using namespace std;
stack<int>s;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
{
int i;
char c;
cin>>i;
s.push(i%10000);
while(cin>>c>>i){
if(c=='+'){
s.push(i%10000);
}else{
int tp=s.top();
s.pop();
s.push(tp*i%10000);
}
}
}
{
int cnt=0;
while(s.size()){
cnt+=s.top();
s.pop();
}
cout<<cnt%10000;
}
return 0;
}
U445242 栈进阶--6 中缀转后缀表达式
#include<iostream>
#include<stack>
#include<unordered_map>
using namespace std;
stack<char>s;
unordered_map<char,int>m;
string in;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
m['(']=0,m['+']=1,m['-']=1,m['*']=2,m['/']=2;
cin>>in;
for(auto i:in){
if(i>='a'&&i<='z'){
cout<<i;
}else{
switch(i){
case '(':{
s.push(i);
break;
}
case ')':{
while(s.top()!='('){
cout<<s.top();
s.pop();
}
s.pop();
break;
}
default:{
while(s.size()&&m[i]<=m[s.top()]){
cout<<s.top();
s.pop();
if(s.empty()){
break;
}
}
s.push(i);
}
}
}
}
while(s.size()){
cout<<s.top();
s.pop();
}
return 0;
}
P1739 表达式括号匹配
#include<iostream>
#include<stack>
using namespace std;
stack<bool>s;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
{
char c;
while(cin>>c){
if(c=='('){
s.push(1);
}else if(c==')'){
if(s.empty()){
cout<<"NO";
return 0;
}
s.pop();
}
}
}
if(s.empty()){
cout<<"YES";
}else{
cout<<"NO";
}
return 0;
}
P1981 [NOIP 2013 普及组] 表达式求值
双 倍 经 验
#include<iostream>
#include<stack>
using namespace std;
stack<int>s;
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
{
int i;
char c;
cin>>i;
s.push(i%10000);
while(cin>>c>>i){
if(c=='+'){
s.push(i%10000);
}else{
int tp=s.top();
s.pop();
s.push(tp*i%10000);
}
}
}
{
int cnt=0;
while(s.size()){
cnt+=s.top();
s.pop();
}
cout<<cnt%10000;
}
return 0;
}
本页面的全部内容在CC BY-NC-ND 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用