团体程序设计天梯赛(L2-037 包装机 (25 分))

题目:

思路分析:

简单的栈和队列的模拟!

第一个代码不知道哪里错了 只有20分 附上 希望有人帮忙发现(感谢大佬们!

代码实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */


const int MAX=10100;
string ss[MAX];
int n,m,s;
stack<char>st;
queue<char>qu;
int main(){
    cin>>n>>m>>s;
    for(int i=1;i<=n;i++){
        cin>>ss[i];
    }
    int x;
    while (cin>>x&&x!=-1) {
        if(x==0){
            if(st.size()==0) continue;
            else {
                char c=st.top();
                st.pop();
                qu.push(c);
            }
        }
        else {
            if(st.size()==s){
                char c=st.top();
                st.pop();
                qu.push(c);
            }
            if(ss[x].length()==0) continue;
            string ans;
            string str=ss[x];
            for(int j=1;j<str.length();j++){
                ans+=str[j];
            }
            ss[x]=ans;
            st.push(str[0]);
        }
    }
    while (!qu.empty()) {
        char c=qu.front();
        qu.pop();
        cout<<c;
    }
}

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */


#include<bits/stdc++.h>
#include<queue>
#include<stack>
using namespace std;
int main() {
    int n,m,s,i,j;
    queue <char> a[101];
    cin>>n>>m>>s;
    char ch;
    for(i=0; i<n; i++) {
        for(j=0; j<m; j++) {
            cin>>ch;
            a[i].push(ch);
        }
    }
    int b;
    stack <char> c1;
    queue <char> c2;
    while(cin>>b) {
        if(b==-1) {
            break;
        }
        if(b!=0) {
            if(a[b-1].size()>0) {
                if(c1.size()>=s) {
                    c2.push(c1.top());
                    c1.pop();
                }

                c1.push(a[b-1].front());
                a[b-1].pop();
            }

        } else {
            if(c1.size()>0) {
                c2.push(c1.top());
                c1.pop();
            }
        }
    }
    int size=c2.size();
    for(i=0; i<size; i++) {
        cout<<c2.front();
        c2.pop();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

郭晋龙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值