版权声明:欢迎转载,转载请标明作者和出处。 https://blog.csdn.net/ljw_study_in_CSDN/article/details/86142505
把字符串从前往后遍历,是#就退栈,是@就清栈,其他的直接入栈
然后把这个栈弄到另外一个栈里,再依次输出另外一个栈的栈顶即可
#include <bits/stdc++.h>
using namespace std;
stack<char>vis1,vis2;
int main()
{
string a;
getline(cin,a);
for(int i=0;i<a.length();i++)
{
vis1.push(a[i]);
char tmp=vis1.top();
vis1.pop();
if(tmp=='@')
{while(!vis1.empty())vis1.pop();}
else if(tmp=='#')
vis1.pop();
else
vis1.push(tmp);
}
while(!vis1.empty())
{
vis2.push(vis1.top());
vis1.pop();
}
while(!vis2.empty())
{
printf("%c",vis2.top());
vis2.pop();
}
printf("\n");
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
int v;
double c;
}liquid;
stack<liquid>vis;
int main()
{
int n,v0,v1,v2,v3;
double c0,c1,c2,c3;
string str;
ios::sync_with_stdio(false);
cin>>v0>>c0>>n;
vis.push({v0,c0});//结构体入栈
while(n--)
{
cin>>str;
if(str=="P")
{
liquid tmp=vis.top();
v1=tmp.v;
c1=tmp.c;
cin>>v2>>c2;
v3=v1+v2;
c3=(v1*c1+v2*c2)/v3;
printf("%d %.5lf\n",v3,c3);
vis.push({v3,c3});
}
if(str=="Z")
{
if(vis.size()==1)
printf("%d %.5lf\n",v0,c0);
if(vis.size()>1)
{
vis.pop();
liquid tmp=vis.top();
printf("%d %.5lf\n",tmp.v,tmp.c);
}
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
stack<int>vis;
int main()
{
int n,x,i=1;
scanf("%d",&n);
while(n--)
{
scanf("%d",&x);
if(i>x)
vis.pop();
if(i<=x)
{
for(;i<=x;i++)
{
vis.push(i);
printf("A");
}
}
printf("B");
}
printf("\n");
return 0;
}
#include <bits/stdc++.h>
using namespace std;
stack<int>dirty,washed,finished;
int main()
{
int i,n,flag,num;
scanf("%d",&n);
for(i=n;i>=1;i--)
dirty.push(i);
while(!(dirty.empty()&&washed.empty()))
//dirty.empty()&&washed.empty()进入循环,否则结束循环
{
scanf("%d%d",&flag,&num);
if(flag==1)
{
while(num--&&!dirty.empty())//注意在num--时还要同时满足 !dirty.empty() 的条件
{
washed.push(dirty.top());
dirty.pop();
}
}
if(flag==2)
{
while(num--&&!washed.empty())
{
finished.push(washed.top());
washed.pop();
}
}
}
while(!finished.empty())
{
printf("%d\n",finished.top());
finished.pop();
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
stack<char>vis;
int main()
{
char a[260];
scanf("%s",a);
int l=strlen(a);
for(int i=0;i<l;i++)
{
if(vis.empty())
vis.push(a[i]);
else
{
if(vis.top()=='('&&a[i]==')'||vis.top()=='['&&a[i]==']')
vis.pop();
else
vis.push(a[i]);
}
}
if(vis.empty()) printf("OK\n");
else printf("Wrong\n");
return 0;
}
先介绍一下string和isdigit,还是很好用的。
string str; 定义变量
cin>>str; 依次读入字符,读入空格或者回车则停止读入。这题字符串没有空格,所以就相当于%s输入了。
str.length()计算字符串s的长度,比如"12345"长度是5。
isdigit(char str)判断一个字符str是否为数字,当str为数字0-9返回非零值,不为数字则返回零。
注释详见代码
#include <bits/stdc++.h>
#define mod 10000
using namespace std;
stack<int>vis;
int main()
{
long long ans=0; //存储答案
long long tmp=0; //存储读入数字的缓冲区
int flag=0; //标记乘号
string str;
cin>>str; //依次读入字符,读入空格或者回车则停止读入。这题字符串没有空格,所以就相当于%s输入了。
str=str+'+'; //在表达式末尾再添加一个加号
for (int i=0;i<=str.length()-1;i++) //在表达式的每一位中
{
if(isdigit(str[i]))//如果是s[i]是数字则累加入缓冲区中,得到数字tmp
tmp=tmp*10+(str[i]-'0');
if(flag&&!isdigit(str[i])) //如果缓冲区数字已读完且数字前是乘号
{
tmp=tmp%mod*vis.top();//累乘栈顶元素,得到数字tmp
vis.pop(); //弹出栈顶元素
flag=0; //取消乘号标记
}
if(str[i]=='+')//如果是加号则将缓冲区数字压入栈中
{
vis.push(tmp%mod);
tmp=0;
}
if(str[i]=='*')//如果是乘号则将缓冲区数字压入栈中并标记乘号
{
flag=1;
vis.push(tmp%mod);
tmp=0;
}
}
while(!vis.empty())
{
ans=ans+vis.top();//累加栈内元素
vis.pop();
}
printf("%d\n",ans%mod);
return 0;
}
今天这六道题就到此为止了。
栈确实还挺好用的,明天学习队列。