Cod sursă (job #817531)

Utilizator avatar Sbora_David Sbora Ioan-David Sbora_David IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp-32 | 1,07 kb
Rundă antrenament Status evaluat
Dată 2 apr. 2025 09:32:37 Scor 100
#include <bits/stdc++.h>

using namespace std;
ifstream fin("bizar.in");
ofstream fout("bizar.out");
string s;
stack<int> stiva; ///-1 pt (

int main()
{
    getline(fin, s);
    int lung = s.size();
    for(int i=0; i<lung; i++) {
        if(s[i] == ' ' || s[i] == ',');
        else if(s[i] == '(') stiva.push(-1);
        else if(isdigit(s[i])) {
            int x = 0;
            while(isdigit(s[i])) x = 10 * x + s[i++] - '0';
            i--, stiva.push(x);
        }
        else {
            ///e )
            vector<int> v;
            while(stiva.top() != -1) v.push_back(stiva.top()), stiva.pop();
            reverse(v.begin(), v.end());
            stiva.pop(); ///scoatem (
            int cnt = stiva.top(); stiva.pop();
            int n = v.size();
            if(cnt % n == 0) cnt = n;
            else cnt %= n;
            stiva.push(v[cnt - 1]);
        }
        ///stack<int> aux = stiva;
        ///while(!aux.empty()) cout << aux.top() << " ", aux.pop();
        ///cout << '\n';
    }
    fout << stiva.top();

    return 0;
}