Cod sursă (job #434686)

Utilizator avatar Ykm911 Ichim Stefan Ykm911 IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1,14 kb
Rundă Arhiva de probleme Status evaluat
Dată 13 feb. 2019 13:34:39 Scor 100
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("bizar.in");
ofstream fout("bizar.out");

char s[100005];
int st[100005], top;

int cifra(char ch)
{
    if('0' <= ch && ch <= '9')
        return 1;
    return 0;
}

int main()
{
    int i, j, cnt = 0, rest, x;
    fin.getline(s, 100005);
    for(i = 0; s[i];)
    {
        if(cifra(s[i]))
        {
            int x = 0;
            while(cifra(s[i]))
            {
                x = x * 10 + (s[i] - '0');
                i++;
            }
            st[++top] = x;
        }
        if(s[i] == '(')
            st[++top] = -1;
        if(s[i] == ')')
        {
            j = top;
            while(st[j] != -1)
            {
                j--;
                cnt++;
            }
            rest = st[j - 1] % cnt;
            if(rest == 0)
                x = st[j + cnt];
            else
                x = st[j + rest];
            top = j - 1;
            st[top] = x;
            cnt = 0;
            i++;
        }
        else i++;
    }
    fout << st[1];
    fin.close();
    fout.close();
    return 0;
}