QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#150779#6395. Equation DiscoveringaestheticCompile Error//C++143.2kb2023-08-26 09:49:092023-08-26 09:49:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-26 09:49:10]
  • 评测
  • [2023-08-26 09:49:09]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
const int MAXN = 22;
int n;
double x[MAXN], y[MAXN];

vector<char> ops;

string genExpr(vector<char> ops)
{
	vector<string> a;	
	while(!ops.empty())
	{
		char c = ops.back();
		if(c == 'x')
		{
			a.push_back("x");
		}
		else if(c == '+' || c == '-' || c == '*' || c == '/')
		{
			string z = a.back();
			a.pop_back();
			string w = a.back();
			a.pop_back();
			if(c == '+')
				a.push_back("(" + z + "+" + w + ")");
			else if(c == '-')
				a.push_back("(" + z + "-" + w + ")");
			else if(c == '*')
				a.push_back("(" + z + "*" + w + ")");
			else if(c == '/')
				a.push_back("(" + z + "/" + w + ")");
		}
		else if(c == 's' || c == 'c')
		{
			string z = a.back();
			a.pop_back();
			if(c == 's')
				a.push_back("sin(" + z + ")");
			else if(c == 'c')
				a.push_back("cos(" + z + ")");
		}
		ops.pop_back();
	}
	return a.back();
}

bool getOut(vector<char> ops, double x, double &ret)
{
	vector<double> a;
	while(!ops.empty())
	{
		char c = ops.back();
		if(c == 'x')
		{
			a.push_back(x);
		}
		else if(c == '+' || c == '-' || c == '*' || c == '/')
		{
			if(a.size() < 2)
				return false;
			double z = a.back();
			a.pop_back();
			double w = a.back();
			a.pop_back();
			if(c == '+')
				a.push_back(z + w);
			else if(c == '-')
				a.push_back(z - w);
			else if(c == '*')
				a.push_back(z * w);
			else if(c == '/')
			{
				if(abs(w) < 0.001)
					return false;
				a.push_back(z / w);
			}
		}
		else if(c == 's' || c == 'c')
		{
			if(a.size() < 1)
				return false;
			double z = a.back();
			a.pop_back();
			if(c == 's')
				a.push_back(sin(z));
			else if(c == 'c')
				a.push_back(cos(z));
		}
		ops.pop_back();
	}
	if(a.size() != 1)
		return false;
	ret = a.back();
	return true;
}

bool check()
{
	if(ops.empty())
		return false;
	for(int i = 1; i <= n; ++i)
	{
		double yy;
		if(!getOut(ops, x[i], yy))
			return false;
		if(abs(yy - y[i]) >= 1e-5)
			return false;
	}
	return true;
}

bool validatePrefix(vector<char> ops)
{
	int cnt = 0;
	for(int i = 0; i < ops.size(); ++i)
	{
		char c = ops[i];
		if(c == 'x')
			cnt++;
		else if(c == '+' || c == '-' || c == '*' || c == '/')
			cnt--;
		if(cnt > 1)
			return false;
	}

	return true;
}

int cost = 0, xs = 0;
void go()
{
	if(cost > 9 || xs > 5)
		return;

	// cout << "Trying: ";
	// for(char c : ops)
	// 	cout << c << ' ';
	// cout << endl;
	if(!validatePrefix(ops))
		return;

	if(check())
	{
		// for(char c : ops)
		// 	cout << c << ' ';
		// cout << endl;

		cout << genExpr(ops) << endl;
		exit(0);
	}


	for(char c : {'x', '+', '-', '*', 's', 'c', '/'})
	{
		ops.push_back(c);
		if(c == '+' || c == '-' || c == '*' || c == '/')
			cost += 2;
		else if(c == 'c' || c == 's')
			cost++;
		else if(c == 'x')
			xs++;
		go();
		if(c == '+' || c == '-' || c == '*' || c == '/')
			cost -= 2;
		else if(c == 'c' || c == 's')
			cost--;
		else if(c == 'x')
			xs--;
		ops.pop_back();
	}
}


int main()
{
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> x[i] >> y[i];
	go();
}ww

Details

answer.code:183:2: error: ‘ww’ does not name a type
  183 | }ww
      |  ^~