Cod sursă (job #152924)

Utilizator avatar torcky Victor Suciu torcky IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1,01 kb
Rundă Arhiva de probleme Status evaluat
Dată 10 aug. 2015 21:04:42 Scor 90
#include <cstdio>
#include <cctype>
using namespace std;
char s[100005];
int poz,v[50005];
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,k=0,x;
    x=findnr();
    if(s[poz]=='('){
        while(s[poz]=='('){
            do{
                v[++k]=solve();
            }while(s[poz]!=')');
            poz++;
            x%=k;
            if(x==0){
                nr=v[k];
            }
            else{
                nr=v[x];
            }
            x=nr;
            k=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;
}