Cod sursă (job #756470)

Utilizator avatar Vlad1Popescu1 Vlad Popescu Vlad1Popescu1 IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp-32 | 0,84 kb
Rundă pre_x_2 Status evaluat
Dată 21 ian. 2024 12:37:16 Scor 100
#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>

std::ifstream in("bizar.in");
std::ofstream out("bizar.out");

int EvalExp(int x)
{
	std::vector<int> secv;

	int currentNr = 0;
	for (char c; ;)
	{
		in >> c;
		if (c == ' ')
			continue;
		if (isdigit(c))
			currentNr = currentNr * 10 + (c - '0');
		else if (c == ',')
			secv.push_back(currentNr), currentNr = 0;
		else if (c == '(')
			currentNr = EvalExp(currentNr);
		else if (c == ')')
		{
			secv.push_back(currentNr);
			currentNr = 0;
			break;
		}
	}

	return secv[(--x % secv.size())];
}

int main()
{
	int x = 0;
	for (char c; in.peek() != '\n';)
	{
		in >> c;

		if (c == ' ')
			continue;
		if (isdigit(c))
			x = x * 10 + (c - '0');
		else if (c == '(')
		{
			x = EvalExp(x);
		}
	}

	out << x;

	return 0;
}