数据结构与算法A实验三栈和队列
7-1 进制转换
答案:
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
char bi[M],oc[M],he[M]; // 数组模拟栈
int nb,no,nh; // 栈顶指针
void get_bin(int n){ // 变为二进制
while(n){
bi[++nb]=n%2+'0';
n/=2;
}
while(nb) cout<<bi[nb--];
}
void get_oct(int n){ // 变为八进制
while(n){
oc[++no]=n%8+'0';
n/=8;
}
while(no) cout<<oc[no--];
}
void get_hex(int n){ // 变为十六进制
while(n){
int k=n%16;
if(k<10) he[++nh]=k+'0';
else he[++nh]=k-10+'A';
n/=16;
}
while(nh) cout<<he[nh--];
}
inline void solve(){
int n;
cin>>n;
if(!n){ // 特判0
cout<<"0 0 0"<<endl;
return ;
}
get_bin(n);cout<<" ";
get_oct(n);cout<<" ";
get_hex(n);cout<<"\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
7-2 表达式转换
坑点:
- 这里的数字可能是小数、负数(负号和数字是一起输出的)或带正号的数字
- 最卡格式的一种情况就是运算符和右括号连续出现的情况比如2+(+5)-1,要仔细考虑输出格式的处理
答案:
#include<bits/stdc++.h>
#define ll long long
#define eb emplace_back
const int N = 2e5 + 10;
using namespace std;
string st[N];
int top;
vector<string>vp;
inline void solve(){
string s;
cin>>s;
bool tag=0;
for(int i=0;i<s.size();i++){
if(isdigit(s[i])){ // 如果是数字直接输出
string str=""; // 判断是否有两位及以上的数字
if(tag) str+='-',tag=0; // 若为负数 带上负号
while((isdigit(s[i])||s[i]=='.')&&i<s.size()){ // 判断是否包含小数
str+=s[i];
i++;
}
i--;
vp.eb(str);
}
else if(s[i]=='('){ // 如果是( 压入栈中
st[++top]=s[i];
if(s[i+1]=='+') i++; // 避免(+5)的正号
else if(s[i+1]=='-') tag=1,i++; // 负数要加上负号
}
else if(s[i]==')'){ // 如果是)
while(top&&st[top]!="(") vp.eb(st[top--]); // 输出()之间的运算符
top--; // 将( 出栈
}
else if(s[i]=='+'||s[i]=='-'){ // 如果是 + -
if(i){
while(top&&st[top]!="(") vp.eb(st[top--]); // 输出(之前的符号
st[++top]=s[i]; // 将现在的符号加入到栈中
}
if(!i&&s[i]=='-') tag=1; // 开头是负数
}
else if(s[i]=='*'||s[i]=='/'){ // 如果是 * /
while(top&&st[top]!="("&&(st[top]=="*"||st[top]=="/")) vp.eb(st[top--]); // 输出(之前的 * /
st[++top]=s[i]; // 将现在的符号加入到栈中
}
}
while(top) vp.eb(st[top--]); // 输出剩余负数
for(int i=0;i<vp.size();i++){ // 输出结果
if(i) cout<<" ";
cout<<vp[i];
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-3 后缀式求值
答案:
#include<bits/stdc++.h>
#define ll long long
#define eb emplace_back
const int N = 2e5 + 10;
using namespace std;
double st[N];
int top;
inline void solve(){
string s;
getline(cin,s);
stringstream ss(s);
while(ss>>s){
if(isdigit(s[0])||s.size()>1) st[++top]=stod(s); // 将数字压栈
else{
if(s=="+") st[top-1]+=st[top],top--; // 加法操作
else if(s=="-") st[top-1]-=st[top],top--; // 减法操作
else if(s=="*") st[top-1]=st[top-1]*1.0*st[top],top--; // 乘法操作
else if(s=="/") st[top-1]=st[top-1]*1.0/st[top],top--; // 除法操作
}
}
cout<<fixed<<setprecision(1)<<st[top]<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-4 括号匹配
答案:
#include<bits/stdc++.h>
#define ll long long
#define eb emplace_back
const int N = 2e5 + 10;
using namespace std;
char st[N];
int top;
inline void solve(){
string s;
getline(cin,s); // 防止出现空格
bool tag=0;
for(int i=0;i<s.size();i++){
if(s[i]=='('||s[i]=='['||s[i]=='{') st[++top]=s[i]; // 左括号入栈
else if(s[i]==')'){ // 如果是(
if(!top||st[top]!='('){ // 队列为空或者栈首不是 ) 则不匹配
tag=1;
break;
}else top--;
}
else if(s[i]==']'){ // 如果是[
if(!top||st[top]!='['){ // 队列为空或者栈首不是 ] 则不匹配
tag=1;
break;
}else top--;
}
else if(s[i]=='}'){ // 如果是{
if(!top||st[top]!='{'){ // 队列为空或者栈首不是 } 则不匹配
tag=1;
break;
}else top--;
}
}
if(top||tag) cout<<"no"<<endl; // 如果最后栈不为空,则不匹配
else cout<<"yes"<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-5 出栈序列的合法性
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
int a[N]; // 数组模拟栈
inline void solve(){
int m,n,k;
cin>>m>>n>>k; // 输入最大容量,入栈元素个数,待检查个数
while(k--){
int top=0; // 定义栈顶
int num=1; // 记录数字
bool tag=0; // 标记
for(int i=1;i<=n;i++){
int x;
cin>>x; // 输入出栈
while(!top||(top<m&&a[top]<x)) a[++top]=num++; // 如果栈为空或者栈顶元素小于x且栈未满,将当前num入栈
if(a[top]==x) top--; // 如果当前栈顶元素为x 则出栈
else tag=1; // 否则不合法
}
if(tag) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-6 栈的实现及基本操作
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
int st[N]; // 数组模拟栈
int top; // 栈顶
inline void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x; // 输入操作
if(!x){
if(!top) cout<<"invalid"<<endl; // 栈为空,则违法
else cout<<st[top]<<endl,top--; // 栈不空,则输出栈顶元素
}else{
int k;
cin>>k;
st[++top]=k; // 元素入栈
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-7 行编辑器
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
char st[N]; // 数组模拟栈
int top; // 栈顶
inline void solve(){
string s;
getline(cin,s); // 输入字符序列,注意“ ”(空格)
for(int i=0;i<s.size();i++){
if(s[i]=='#'){ // 退格符
if(top) top--;
}else if(s[i]=='@') top=0; // 退行符
else st[++top]=s[i]; // 字符入栈
}
for(int i=1;i<=top;i++) cout<<st[i]; // 输出字符序列
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-8 走迷宫
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
struct node{
int x,y,num;
};
int n,m;
int sx,sy; // 起点坐标
int ex,ey; // 终点坐标
int G[M][M]; // 存迷宫
bool st[M][M]; // 标记数组
void BFS(){
queue<node>q;
q.push({sx,sy,1}); // 将起点放入
st[sx][sy]=1;
while(q.size()){
auto t=q.front();
q.pop();
int xx,yy;
for(int i=0;i<4;i++){ // 上下左右移动
xx=t.x+dx[i];
yy=t.y+dy[i];
if(xx<1||xx>n||yy<1||yy>m) continue; // 超出尺寸,不判断
if(st[xx][yy]) continue; // 该点已走过不走,保证最短性
if(G[xx][yy]==1) continue; // 障碍物不走
if(G[xx][yy]==4){ // 到达终点输出结果
cout<<t.num<<endl;
return ;
}
if(!st[xx][yy]&&!G[xx][yy]){ // 没走过且当前点是空地
st[xx][yy]=1;
q.push({xx,yy,t.num+1}); // 入队
}
}
}
cout<<"unreachable"<<endl; // 走不到终点 输出
}
inline void solve(){
cin>>n>>m; // 输入长和宽
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>G[i][j];
if(G[i][j]==3) sx=i,sy=j; // 记录起点
if(G[i][j]==4) ex=i,ey=j; // 记录终点
}
}
BFS(); // BFS搜索答案
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-9 银行业务队列简单模拟
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
queue<int>A,B; // A\B队列
vector<int>vp; // 记录答案
inline void solve(){
int n;
cin>>n;
int x;
for(int i=1;i<=n;i++){
cin>>x;
if(x&1) A.push(x); // 如果是奇数,进入A队列
else B.push(x); // 如果是偶数,进入B队列
}
while(A.size()&&B.size()){ // 模拟处理过程
if(A.size()>1){ // 若A中有不少于两个元素,则A出2,B出1
vp.eb(A.front());A.pop();
vp.eb(A.front());A.pop();
vp.eb(B.front());B.pop();
}else if(A.size()==1){ // 若A中有一个元素,则A出1,B出1
vp.eb(A.front());A.pop();
vp.eb(B.front());B.pop();
}
}
while(A.size()) {vp.eb(A.front());A.pop();} // 若B中元素全部运行完,A中有剩余继续处理A
while(B.size()) {vp.eb(B.front());B.pop();} // 若A中元素全部运行完,B中有剩余继续处理B
for(int i=0;i<vp.size();i++){ // 输出答案
if(i) cout<<" ";
cout<<vp[i];
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-10 队列模拟
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define x first
#define y second
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eb emplace_back
const double eps = 1e-6;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
using namespace std;
int q[N]; // 数组模拟队列
int head,tail; // 队列队首指针、队尾指针
inline void solve(){
int x;
bool tag=0;
while(cin>>x&&x){ // 输入元素
if(x>0) q[++tail]=x; // 进入队列
else if(x<0){ // 元素出队
if(head>=tail) tag=1; // 队列为空,报错
else head++; // 队列不空 出队
}
}
if(tag){
cout<<"wrong"<<endl;
return ;
}
if(tail-head+1>=10) cout<<"Queue is full!"<<endl; // 判断队列是否满队
cout<<"The Queue is:";
for(int i=head+1;i<=min(head+10,tail);i++) cout<<q[i]<<" "; // 输出队列结果
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}