L2-037包装机(25分)(简单模拟+栈的应用)

AC代码:

#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 105;
queue<char> v[maxn]; ///存储每个轨道上的物品
stack<char> s;  ///筐
queue<char> q;  ///结果输出
int main()
{
    int N,M,S;
    int a[1005];
    char ch;
    cin>>N>>M>>S;
    getchar();
    for(int i=1;i<=N;i++){
        for(int j=0;j<M;j++){
            cin>>ch;
            v[i].push(ch);
            a[i]++;
        }
        getchar();
    }
    int x;
    while(true){
        cin>>x;
        if(x==-1)break;
        if(x==0){
            if(s.size()==0)continue;	//如果筐里面的为空就不做任何处理,跳过
            else{
                q.push(s.top());		//否则,弹出栈顶元素,放入流水线上
                s.pop();
            }
        }
        if(x>=1 && x<=N){
            if(v[x].size()==0)continue;		//如果对应轨道上没有物品了不做任何处理,跳过
            else{
                if(s.size()==S){	//如果筐已经满了,就弹出筐顶物品到流水线上,再将物品放入框中
                    q.push(s.top());
                    s.pop();
                }
                s.push(v[x].front());
                v[x].pop();
            }
        }
    }
    while(!q.empty()){
        cout<<q.front();
        q.pop();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值