L2-037 包装机 (25 分)

本文解析了如何利用栈和队列数据结构解决实际问题,通过实例展示了在给定代码中栈与队列的交互操作,适合深入理解这两种数据结构在算法中的运用。

思路

上面的轨道可以看作若干的队列,下面的筐可以看作栈,问题就简化成了栈和队列的操作。

代码

#include<iostream>
#include<queue>
#include<stack>
#include<string> 

using namespace std;
queue<char> q[105];
stack<char> s;
string ans = "";
int main(){
	int n, m, smax;
	cin>>n>>m>>smax;
	for(int i=1; i <= n; i++){
		for(int j=0; j < m; j++){
			char a;
			cin>>a;
			q[i].push(a);
		}
	}
	while(1){
		int opt;
		cin>>opt;
		if(opt == -1){
			break;
		}
		if(opt){
			if(s.size() == smax){
				if(!q[opt].empty()){
					ans += s.top();
					s.pop();
					char a = q[opt].front();
					q[opt].pop();
					s.push(a);
				}
				
			}else{
				if(!q[opt].empty()){
					char a = q[opt].front();
					q[opt].pop();
					s.push(a);	
				}	
			}
		}else if(!s.empty()){
			ans += s.top();
			s.pop();
		}
	}
	cout<<ans<<endl;
	return 0;
}

其他题目

天梯L2部分题解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾醒(AiXing-w)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值