数据结构-栈练习-题解
A 7887:括号匹配
太简单了,不讲
B 7676:括弧匹配检验
#include<bits/stdc++.h> using namespace std; int find(char a[]) { char s[305]; int top = -1; for(int i=0;i<strlen(a);i++) { if(a[i]=='(' || a[i]=='[' || a[i]=='{') s[++top] = a[i]; else if(a[i]==')' && s[top]!='(')return 0; else if(a[i]==']' && s[top]!='[')return 0; else if(a[i]=='}' && s[top]!='{')return 0; else top--; } if(top!=-1)return 0; else return 1; } int main() { char a[300]; cin>>a; if(find(a))cout<<"OK"; else cout<<"Wrong"; return 0; }
C 5755:字符串匹配问题
#include<bits/stdc++.h> using namespace std; int yxj(char c) { switch(c) { case '(':return 2;break; case '[':return 3;break; case '{':return 4;break; case '<':return 1;break; default:return 0;break; } } int find(char a[]) { stack<char>q; if(yxj(a[0])==0)return 0; q.push(a[0]); for(int i=1;i<strlen(a);i++) { if(yxj(a[i])) //满足条件证明a[i]是左括号 { if(q.empty())q.push(a[i]); //看这里******************* else if(yxj(q.top()) >= yxj(a[i]))q.push(a[i]); else return 0; //优先级不匹配 } else{ //a[i]是右括号 if(q.empty())return 0; //没有与之匹配的左括号 else if(a[i]==')' && q.top()!='(')return 0; else if(a[i]==']' && q.top()!='[')return 0; else if(a[i]=='}' && q.top()!='{')return 0; else if(a[i]=='>' && q.top()!='<')return 0; else q.pop(); } } if(q.empty())return 1; else return 0; } int main() { int n; cin>>n; while(n--) { char a[300]; cin>>a; if(find(a))cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
D 7499:过滤多余的空格
结合输入的规律,一定是输入句子后输入字符,所以输入句子后 用 getchar获取字符来判断字符是否是回车,是的话证明这一行的输入结束了
#include <iostream> #include <stack> #include <cstring> using namespace std; int main() { char s[205]; while(cin >> s) { cout << s; if(getchar() == '\n') { cout << endl; continue; } while (cin >> s) { char c = getchar(); cout << " " << s; if (c == '\n') { cout << c; break; } } } return 0; }
E 5014:说反话
结合getchar来判断输入是否结束了,将每个单词压入栈中,最后再依次弹出,请大家向这位同学学习,检验自己freopen的使用是否过关~
#include<bits/stdc++.h> using namespace std; stack<string> s; char a[100]; int main(){ //freopen("1005.in","r",stdin); //freopen("1005.out","w",stdout); while(cin>>a){ char c=getchar(); s.push(a); if(c=='\n')break; } cout<<s.top(); s.pop(); //因为每个之间有空格隔开,所以需要先输出一个单词,后面的都输出空格+单词即可 while(!s.empty()){ cout<<' '<<s.top(); s.pop(); } return 0; }
F 7675:车厢调度
用数组模拟等待入栈的队列q,每次输入x都让小于等于x的元素入栈,然后再判断栈顶是否和x相同,是的话就栈顶弹出,否则就证明不是合法出栈顺序
#include<bits/stdc++.h> using namespace std; int main() { int q[1001]; int head = 1,n,tail,f = 1; cin>>n; for(int i=1;i<=n;i++)q[i] = i;//入栈元素按顺序构成队列q tail = n; //队尾tail 为长度n stack<int>s; //栈中s for(int i=1;i<=n;i++) { int x; cin>>x; //x要出栈,那么小于等于x的所有都要入栈 while(head<=tail&&q[head]<=x) //当队列没有入栈完,队列中还有小于等于x的元素时 { s.push(q[head]); //队首 入栈 head++; // 队首后移 } if(s.empty() || s.top()!=x){ //如果出现入栈操作完成后栈中为空或栈顶不为x,那么证明出栈顺序错误 f = 0;break; }else s.pop(); //否则证明x符合出栈顺序,从栈顶出栈 } if(f)cout<<"YES"; else cout<<"NO"; return 0; }
G 7642:逆波兰表达式
#include<bits/stdc++.h> using namespace std; double cal(double x,double y,char op) { switch(op) { case '+':return x+y;break; case '-':return y-x;break; case '*':return y*x;break; case '/':return y/x;break; } } int main() { char a[101],c; stack<double> q; cin>>a; q.push(atof(a)); //字符串a转小数入栈 while(c=getchar()) { if(c=='\n')break; cin>>a; if(a[0]>='0' && a[0]<='9')q.push(atof(a)); //数字入栈 else{ //否则是运算符 double x = q.top();q.pop(); double y = q.top();q.pop(); double z = cal(x,y,a[0]);//从栈中取出两个元素,进行运算后将结果压回栈中 q.push(z); } } printf("%.2f",q.top()); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现