Cod sursă (job #152927)

Utilizator avatar torcky Victor Suciu torcky IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1,12 kb
Rundă Arhiva de probleme Status evaluat
Dată 10 aug. 2015 21:35:01 Scor 100
#include <cstdio>
#include <cctype>
using namespace std;
char s[100005];
int poz,v[100005],k=1;
int findnr(){
    int nr=0;
    while(!isdigit(s[poz])){
        poz++;
    }
    while(isdigit(s[poz])){
        nr*=10;
        nr+=s[poz];
        nr-='0';
        poz++;
    }
    return nr;
}
int solve(){
    int nr,x,counter=0;
    x=findnr();
    if(s[poz]=='('){
        while(s[poz]=='('){
            k--;
            do{
                v[++k]=solve();
                counter++;
            }while(s[poz]!=')');
            poz++;
            x%=k;
            if(x==0){
                nr=v[k];
            }
            else{
                nr=v[x+k-counter];
            }
            x=nr;
            k-=counter;
            k++;
            counter=0;
        }
    }
    else{
        nr=x;
    }
    return nr;
}
int main()
{
    freopen("bizar.in","r",stdin);
    freopen("bizar.out","w",stdout);
    int n=0;
    char c;
    do{
        c=getc(stdin);
        if(c!=' '){
            s[n++]=c;
        }
    }while(c!='\n');
    printf("%d\n",solve());
    return 0;
}