1 条题解

  • 0
    @ 2023-6-11 12:20:57

    C++ :

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    using namespace std;
    int flag[10010];
    int main()
    {
        deque<int >q;
        int n,m,i,k;
        char str[10];
        memset(flag,0,sizeof(flag));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%s",str);
            if(strcmp(str,"LIN")==0)
            {
                cin>>m;
                q.push_front(m);
            }
            else if(strcmp(str,"RIN")==0)
            {
                cin>>m;
                q.push_back(m);
            }
            else if(strcmp(str,"LOUT")==0)
            {
                if(!q.empty())
                    q.pop_front();
                else
                    flag[i]=1;
            }
            else if(strcmp(str,"ROUT")==0)
            {
                if(!q.empty())
                    q.pop_back();
                else
                    flag[i]=1;
            }
    
        }
            k=q.front();
            q.pop_front();
            cout<<k<<' ';
            while(!q.empty())
            {
                k=q.front();
                q.pop_front();
                cout<<k<<' ';
            }
            cout<<endl;
             for(i=1;i<=n;i++)
            {
                if(flag[i])
                    cout<<i<<" "<<"ERROR"<<endl;
            }
    }
    
    

    Java :

    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int i, n = sc.nextInt(), t;
            String ac = null;
            boolean[] errors = new boolean[10001];
            Deque<Integer> dq = new ArrayDeque();
            for (i = 1; i <= n; i++) {
                ac = sc.next();
                errors[i] = false;
                if ("LIN".equals(ac)) {
                    t = sc.nextInt();
                    dq.addFirst(t);
                } else if ("RIN".equals(ac)) {
                    t = sc.nextInt();
                    dq.addLast(t);
                } else if ("LOUT".equals(ac)) {
                    if (dq.size() > 0) dq.removeFirst();
                    else {
                        errors[i] = true;
                    }
                } else if ("ROUT".equals(ac)) {
                    if (dq.size() > 0) dq.removeLast();
                    else {
                        errors[i] = true;
                    }
                }
            }
            while (dq.size() > 0) {
                System.out.print(dq.getFirst() + " ");
                dq.pop();
            }
            System.out.println();
            for (i = 1; i <= n; i++) {
                if (errors[i] == true) {
                    System.out.println(i+" ERROR");
                }
            }
            sc.close();
        }
    }
    
    
    • 1

    信息

    ID
    2642
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者