题解

有问题又不好意思问的,可以直接在底下留言,只要我看到了,我就会回。

 

A

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

using namespace std;
int n, m, x;
string s, ss;

int main(){
    cin >> n;
    while(n--){
        queue<int> q;
        stack<int> st;
        cin >> m >> s;
        if(s == "FIFO"){
            for(int i = 0; i < m; i++){
                cin >> ss;
                if(ss == "IN"){
                    cin >> x;
                    q.push(x);
                }else{
                    if(q.empty()){
                        cout << "None" << endl;
                        continue;
                    }
                    cout << q.front() << endl;
                    q.pop();
                }
            }
        }else{
            for(int i = 0; i < m; i++){
                cin >> ss;
                if(ss == "IN"){
                    cin >> x;
                    st.push(x);
                }else{
                    if(st.empty()){
                        cout << "None" << endl;
                        continue;
                    }
                    cout << st.top() << endl;
                    st.pop();
                }
            }
        }

    }
    return 0;
}
View Code

 

B

#include <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
int n;
string s, ss;
stack<char> st;
int vis[1000];

int main(){
    while (cin >> n >> s >> ss){
        memset(vis, 0, sizeof(vis));
        while(!st.empty())
            st.pop();
        int cnt = 0;
        int j = 0;
        for(int i = 0; i < n; i++){
            st.push(s[i]);
            vis[cnt++] = 1;
            while(j < n){
                if(!st.empty() && st.top() == ss[j]){
                    st.pop();
                    j++;
                    vis[cnt++] = 0;
                }else
                    break;
            }
        }
        if(j == n){
            cout << "Yes." <<endl;
            for(int i = 0; i < cnt; i++){
                if(vis[i])
                    cout << "in" << endl;
                else
                    cout << "out" << endl;
            }
        }else{
            cout<< "No." << endl;
        }
        cout << "FINISH" <<endl;
    }
    return 0;
}
View Code

 

C

#include <iostream>
#include <stack>
#include <stdio.h>
using namespace std;
string s;
int main(){
    while(getline(cin, s) && s != "0"){
        //cout << s << e    ndl;
        stack<double> s1;
        stack<char> s2;
        int len = s.length();
        for(int i = 0; i < len; i++ ){
            if(isdigit(s[i])){
                double an = 0;
                while(isdigit(s[i])){
                    an = an*10 + s[i] - '0';
                    i++;
                }
                s1.push(an);
            }else if(s[i] == '+' || s[i] == '-'){
                if(s2.empty())
                    s2.push(s[i]);
                else{
                    char ss = s2.top();
                    s2.pop();
                    double a = s1.top();
                    s1.pop();
                    double b = s1.top();
                    s1.pop();
                    double ans = ss == '+'? a+b : b-a;
                    s1.push(ans);
                    s2.push(s[i]);
                }
            }else if(s[i] == '*' || s[i] == '/'){
                char ss = s[i];
                i += 2;
                double an = 0;
                while(isdigit(s[i])){
                    an = an*10 + s[i] - '0';
                    i++;
                }
                double bn = s1.top();
                s1.pop();
                double ans = ss == '*' ? an*bn : bn/an;
                s1.push(ans);
            }
        }
        while(!s2.empty()){
            char ss = s2.top();
            s2.pop();
            double a = s1.top();
            s1.pop();
            double b = s1.top();
            s1.pop();
            double ans = ss == '+'? a+b : b-a;
            s1.push(ans);
        }
        printf("%0.2f\n", s1.top());
    }
    return 0;
}
View Code

 

E

#include <iostream>
#include <queue>

using namespace std;

string s;
int cnt = 0;
struct Node{
    int val, pri, index;
    string ss;
    friend bool operator < (const Node &a, const Node &b){
        if(a.pri == b.pri)
            return a.index > b.index;
        else
            return a.pri > b.pri;
    }
};

priority_queue<Node> q;

int main(){
    Node ans;
    while(cin >> s){
        if(s == "GET"){
            if(!q.empty()){
                ans = q.top();
                q.pop();
                cout << ans.ss << " " << ans.val << endl;
            }else{
                cout << "EMPTY QUEUE!" << endl;
            }

        }else{
            cin >> ans.ss >> ans.val >> ans.pri;
            ans.index = cnt++;
            q.push(ans);
        }
    }
    return 0;
}
View Code

 

 

G

#include <iostream>
#include <queue>
#include <map>
#define N 1050
using namespace std;
queue<int> q[N], qt;
map<int,int> mp;
int n, m;
string s;
int main(){
    int t;
    int pos = 0;
    while(cin >> t && t){
        pos ++;
        cout << "Scenario #" << pos << endl;
        for(int i = 0; i < t; i ++){
            cin >> n;
            for(int j = 0; j < n; j ++){
                cin >> m;
                mp[m] = i;
            }
        }
        while(cin >> s){
            int x;
            if(s[0] == 'E'){
                cin >> x;
                int an = mp[x];
                if(q[an].empty())
                    qt.push(an);
                q[an].push(x);
            }else if(s[0] == 'D'){
                int an = qt.front();
                if(!q[an].empty()){
                    cout << q[an].front() << endl;
                    q[an].pop();
                }
                if(q[an].empty())
                    qt.pop();
            }else{
                for(int i = 0; i < t; i++)
                    while(!q[i].empty())
                        q[i].pop();
                while(!qt.empty())
                    qt.pop();
                cout << endl;
                break;
            }
        }
    }
    return 0;
}
View Code

 

posted @ 2019-03-17 09:30  #忘乎所以#  阅读(324)  评论(0编辑  收藏  举报