23蓝桥杯ACM培训-栈

前言:

回校第二天训练,今天的题目主要与stack有关。

正文:

Problem:A 栈-程序员输入问题:

#include<bits/stdc++.h>
using namespace std;
int main(){
	stack<char> s1;
	stack<char> s2;
	string str;
	getline(cin,str);
	int len=str.size();
	for(int i=0;i<len;i++)
    {
        if(str[i]=='@')
        {
            while(!s1.empty())
            {
              s1.pop();
            }
            continue;
        }
		if(str[i]=='#')
        {
          if(!s1.empty())
          {
              s1.pop();continue;
          }
        }
        s1.push(str[i]);
    }
    while(!s1.empty())
    {
        s2.push(s1.top());
        s1.pop();
    }
    while(!s2.empty())
    {
        printf("%c",s2.top());
        s2.pop();
    }
    printf("\n");
    return 0;
}

注意输入有空格。用两个stack,其中一个先接受输入的字符,遇到#就弹出栈顶,遇到@就将栈全布元素都弹出,之后用另一个stack将顺序恢复并输出。

Problem:B 栈-溶液模拟器:

#include <bits/stdc++.h>
using namespace std;
typedef struct{
	int v;
	double c;
}ly;
stack<ly> x;
int main(){
	int v0,c0;
	int n;
	cin>>v0>>c0;x.push({v0,c0});
	cin>>n;
	while(n--){
		string str;
		cin>>str;
		if(str=="P"){
			ly tmp=x.top();
			int v1,v2,v3;double c1,c2,c3;cin>>v1>>c1;
			v2=tmp.v;c2=tmp.c;
			v3=v1+v2;c3=(v1*c1+v2*c2)/v3;
			x.push({v3,c3});
			printf("%d %.5lf\n",v3,c3);
		}
		if(str=="Z"){
			if(x.size()==1){
				printf("%d %.5lf\n",v0,c0);
			}
			else{
				x.pop();
                ly tmp=x.top();
                printf("%d %.5lf\n",tmp.v,tmp.c);
			}
		}
	}
	return 0;
}

重新定义一个结构体,建立一个stack用于储存这个结构体,再根据操作实时更新stack,为p就更新答案,为z就弹出顶部答案(没上次操作就弹出v0,c0)。

Problem:C 栈-火车编组:

#include <bits/stdc++.h>
using namespace std;
int n,p,a[110];
stack<int>s;
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    p=1;
    for(int i=1;i<=n;i++)
    {
        if(i<=a[p]){s.push(i);printf("A");}
        if(i==a[p])
        {
            while(!s.empty()&&s.top()==a[p])
            {
                s.pop();
                printf("B");
                p++;
            }
            if(p==n+1)break;
        }
    }
    printf("\n");
    return 0;
}

在数据合理的情况下我们只需要注意p值(相对应数的位置)的更改即可。

Problem:D 栈-洗盘子:

#include <bits/stdc++.h>
using namespace std;
stack<int>s1,s2,s3;
int main()
{
    int n,x,flag;
    cin>>n;
    for(int i=n;i>=1;i--)
        s1.push(i);
    while(!(s1.empty()&&s2.empty()))
    {
        cin>>flag>>x;
        if(flag==1)
        {
            while(x--&&!s1.empty())
            {s2.push(s1.top());s1.pop();}
        }
        if(flag==2)
        {
            while(x--&&!s2.empty())
            {s3.push(s2.top());s2.pop();}
        }
    }
    while(!s3.empty())
    {printf("%d\n",s3.top());s3.pop();}
    return 0;
}

很直观易懂的题目,一步一步慢慢写就行了。

Problem:E 栈-括号匹配:

#include<bits/stdc++.h>
using namespace std;
int main(){
	stack<char> k;
	string s;
	cin>>s;
	int l=s.size();
	char tmp;
	for(int i=0;i<l;i++){
		if(k.empty()){
			k.push(s[i]);
		}
		else{
			tmp=k.top();
			if((tmp=='['&&s[i]==']')||(tmp=='('&&s[i]==')')){
				k.pop();
			}
			else{
				k.push(s[i]);
			}
		}
		}
	if(k.empty()){
		cout<<"OK"<<endl;
	}
	else cout<<"Wrong"<<endl;
	return 0;
}

一时未匹配的括号先在stack里储着,如果有匹配括号就弹出,另一边也不用储入,遍历完后检查stack中是否为空即可。

后记:

  老师今天说寒假没努力等比赛就只能当分母了,瞬间感觉自己中了一枪,寒假前信誓旦旦的说自己要在家好好学习的,结果摆了整整两个月,我的自制力果然有待加强。如今已经返校了,在校园内自己一定要把握好剩下的时间,让自己有更大的进步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值