Cod sursă (job #435042)

Utilizator avatar iustin948 Homoranu Iustin iustin948 IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1,28 kb
Rundă Arhiva de probleme Status evaluat
Dată 13 feb. 2019 19:27:30 Scor 90
#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

ifstream fin("bizar.in");
ofstream fout("bizar.out");
char s[100010];
int st[100010], lg;
int top;
void Stiva()
{
    int i;
    fin.getline(s,100005);
    lg = strlen(s);
    for(i = 0; i < lg; i++)
        if(s[i] == '(')
           st[++top] = -1;
        else if(s[i] >= '0' && s[i] <= '9')
        {
            int x = 0;
            while(s[i] >= '0' && s[i] <= '9')
                {
                    x = x * 10 + (s[i] - '0');
                    i++;
                }
                i--;
            st[++top] = x;
        }
        else if(s[i] == ')')
        {
            int x = 0, k;
            k = top;
            while(st[top] != -1)
                top--, x++;
            top--;
            if(st[top] < k - top - 1)
                {
                    st[top] = st[top + (k - top - 1) - 1];
                        //cout << k - top - 1 << "\n";
                }
                else if(st[top] % (k - top - 1) != 0) st[top] = st[top + (st[top] % (k - top - 1)) + 1 ];
                else st[top] = st[top + (k - top) ];
            //cout << st[top] << "\n";
        }


}

int main()
{
    Stiva();
    fout << st[top];
    return 0;
}