Cod sursă (job #128399)

Utilizator avatar tcostinr Tudor Costin Razvan tcostinr IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1.01 kb
Rundă Arhiva de probleme Status evaluat
Dată 4 mar. 2015 18:28:16 Scor 100
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define NMAX 110000

int p,N;
char str[NMAX];

int expresie(int);

int main()
{
freopen("bizar.in","r",stdin);
freopen("bizar.out","w",stdout);

gets(str);
N=strlen(str);

printf("%d\n",expresie(1));

return 0;
}

int expresie (int x)
{
    vector < int > e;

    while (p<N && str[p]!=')')
    {
        if (str[p]=='(')
        {
            int t=e[e.size()-1];
            e.pop_back();
            ++p;
            e.push_back(expresie(t));
        }

        if (str[p]==',' || str[p]==' ')
        {
            ++p;
            continue;
        }

        if ('0'<=str[p] && str[p]<='9')
        {
            int n=0;

            while ('0'<=str[p] && str[p]<='9')
            {
                n=n*10+str[p]-'0';
                ++p;
            }
            e.push_back(n);
        }
    }

    x=x%e.size();
    ++p;

    if (x==0) return e[e.size()-1];
    else return e[x-1];
}