eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Черга необмеженого розміру

опубліковано 04.01.24, 21:14:35

include <bits/stdc++.h>

using namespace std; using ll = long long;

int main() { iosbase::syncwith_stdio(0); cin.tie(0); string s; queue<int> st; int x; while(cin >> s){ if(s == "exit"){ cout << "bye"; return 0; } if(s == "push"){ cin >> x; st.push(x); cout << "ok"; } if(s == "pop"){ if(st.empty()) cout << "error"; else { cout << st.front(); st.pop(); } } if(s == "front"){ if(st.empty()) cout << "error"; else cout << st.front(); } if(s == "size"){ cout << st.size(); } if(s == "clear"){ while(!st.empty()) st.pop(); cout << "ok"; } cout << endl; } }

опубліковано 03.04.24, 02:20:58

include <iostream>

include <queue>

include <string>

using namespace std;

class Queue { private: queue<int> q;

public: void push(int item) { q.push(item); cout << "ok" << endl; }

void pop() {
    if (!q.empty()) {
        cout << q.front() << endl;
        q.pop();
    } else {
        cout << "error" << endl;
    }
}

void front() {
    if (!q.empty()) {
        cout << q.front() << endl;
    } else {
        cout << "error" << endl;
    }
}

void size() {
    cout << q.size() << endl;
}

void clear() {
    while (!q.empty()) {
        q.pop();
    }
    cout << "ok" << endl;
}

};

int main() { Queue queue;

string command;
int value;

while (cin >> command) {
    if (command == "push") {
        cin >> value;
        queue.push(value);
    } else if (command == "pop") {
        queue.pop();
    } else if (command == "front") {
        queue.front();
    } else if (command == "size") {
        queue.size();
    } else if (command == "clear") {
        queue.clear();
    } else if (command == "exit") {
        cout << "bye" << endl;
        break;
    }
}

return 0;

}

опубліковано 03.04.24, 02:30:55

include <iostream>

include <queue>

include <string>

using namespace std;

class Queue { private: queue<int> q;

public: void push(int item) { q.push(item); cout << "ok" << endl; }

void pop() {
    if (!q.empty()) {
        cout << q.front() << endl;
        q.pop();
    } else {
        cout << "error" << endl;
    }
}

void front() {
    if (!q.empty()) {
        cout << q.front() << endl;
    } else {
        cout << "error" << endl;
    }
}

void size() {
    cout << q.size() << endl;
}

void clear() {
    while (!q.empty()) {
        q.pop();
    }
    cout << "ok" << endl;
}

};

int main() { Queue queue;

string command;
int value;

while (cin >> command) {
    if (command == "push") {
        cin >> value;
        queue.push(value);
    } else if (command == "pop") {
        queue.pop();
    } else if (command == "front") {
        queue.front();
    } else if (command == "size") {
        queue.size();
    } else if (command == "clear") {
        queue.clear();
    } else if (command == "exit") {
        cout << "bye" << endl;
        break;
    }
}

return 0;

}