Pentru această operație este nevoie să te autentifici.
Cod sursă (job #97993)
Utilizator |
|
IP | ascuns |
---|---|---|---|
Problemă | Bizar (clasele 9-10) | Compilator | cpp | 0,90 kb |
Rundă | Tema 10 clasele 9-10 2014/15 | Status | evaluat |
Dată | 14 dec. 2014 14:46:27 | Scor | 100 |
#include <stdio.h>
#include <ctype.h>
#include <vector>
#define N_MAX 100000
using namespace std;
FILE *fin, *fout;
char str[N_MAX + 2]; // Inca 2 elemente pentru '\n' si '\0'
int size, sp;
/*
Gramatica expresiei:
E = N [L]*
L = '(' [E ',']* ')'
*/
int E();
int N();
vector<int> L();
int E()
{
int ans = N();
while (str[sp] == '(') {
vector<int> v = L();
ans = v[(ans - 1) % v.size()];
}
return ans;
}
vector<int> L()
{
vector<int> ans;
sp++; // Sarim peste '('
while (str[sp - 1] != ')') {
ans.push_back(E());
sp++;
}
return ans;
}
int N()
{
int ans = 0;
while (isdigit(str[sp])) {
ans = ans * 10 + str[sp++] - '0';
}
return ans;
}
int main()
{
fin = fopen("bizar.in", "r");
fout = fopen("bizar.out", "w");
// Citire
while ((str[size++] = fgetc(fin)) != '\n') {
if (str[size - 1] == ' ') {
size--;
}
}
fprintf(fout, "%d", E());
fclose(fin);
fclose(fout);
return 0;
}