Cod sursă (job #97254)

Utilizator avatar george-liviu Pereteanu George george-liviu IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1,48 kb
Rundă Tema 10 clasele 9-10 2014/15 Status evaluat
Dată 11 dec. 2014 15:03:02 Scor 70
#include <cstdio>

using namespace std;

const int MAXSIR = 100001 ;

char sir[MAXSIR] ;

int mstivoasa[1001][1001] ;
int nivel = 1 ;

void citire()
{
    freopen("bizar.in","r",stdin);
    freopen("bizar.out","w",stdout);
    gets(sir) ;
}

int calcul()
{
    if( mstivoasa[nivel - 1][mstivoasa[nivel - 1][0]] % mstivoasa[nivel][0] == 0 )
        return mstivoasa[nivel][mstivoasa[nivel][0]] ;
    return mstivoasa[nivel][mstivoasa[nivel - 1][mstivoasa[nivel - 1][0]] % mstivoasa[nivel][0]] ;
}

void prelucrare()
{
    int nr = 0 ;
    for ( int i = 0 ; sir[i] != 0 ; i++ )
    {
        if ( sir[i] == ' ' ) continue;
        if ( sir[i] >= '0' && sir[i] <= '9' )
        {
            nr = nr * 10 + sir[i] - '0' ;
        }
        if ( sir[i] == '(' )
        {
            if ( nr != 0 )
            mstivoasa[nivel][++mstivoasa[nivel][0]] = nr ;
            nr = 0 ;
            nivel++;
            mstivoasa[nivel][0] = 0 ;
        }
        if ( sir[i] == ',' )
        {
            if ( nr != 0 )
            mstivoasa[nivel][++mstivoasa[nivel][0]] = nr ;
            nr = 0 ;
        }
        if ( sir[i] == ')' )
        {
            if ( nr != 0 )
            mstivoasa[nivel][++mstivoasa[nivel][0]] = nr ;
            mstivoasa[nivel - 1 ][mstivoasa[nivel - 1 ][0]] = calcul();
            nivel--;
            nr = 0 ;

        }

    }
}

int main()
{
    citire();
    prelucrare();
    printf("%d\n",mstivoasa[1][1]);
}