洛谷评测姬怎么了?调对赏关!
  • 板块P1236 算24点
  • 楼主EricWan
  • 当前回复8
  • 已保存回复8
  • 发布时间2023/7/7 13:09
  • 上次更新2023/11/3 11:12:54
查看原帖
洛谷评测姬怎么了?调对赏关!
377873
EricWan楼主2023/7/7 13:09

本地可过,洛谷评测全 No Answer!?

#include <bits/stdc++.h>
#define int long long
#define ith *(&a + i - 1)
#define jth *(&a + j - 1)
#define back s = s.substr(0,nl);
#define fuyuan a=e,b=f,c=g,d=h
using namespace std;
int a, b, c, d;
string s;
string nts(int k)
{
//	cout << k << "~";
	string ans = "";
	if (k < 10)
	{
//		cout << ans + char(k + '0') << "!";
		return ans + char(k + '0');
	}
//	cout << ans + char(k / 10 + '0') + char(k + '0') << "!";
	return ans + char(k / 10 + '0') + char(k % 10 + '0');
}
void dfs(int a, int b, int c, int d, int step)
{
	sort(&a,&a + 4);
	int nl = s.size();
	if (a < 0)
	{
		return;
	}
	int e = a, f = b, g = c, h = d;
//	cout << a << " " << b << " " << c << " " << d << endl << s;
	if (step == 1)
	{
		if (d == 24)
		{
			cout << s;
			exit(0);
		}
		return;
	}
	for (int i = 4 - step + 2; i <= 4; i++)
	{
		for (int j = 4 - step + 1; j < i; j++)
		{
			s += nts(ith) + "+" + nts(jth) + "=" + nts(ith + jth) + "\n";
			ith += jth;
			jth = 0;
			dfs(a,b,c,d,step - 1);
			back;
			fuyuan;
			s += nts(ith) + "-" + nts(jth) + "=" + nts(ith - jth) + "\n";
			ith -= jth;
			jth = 0;
			dfs(a,b,c,d,step - 1);
			back;
			fuyuan;
			s += nts(ith) + "*" + nts(jth) + "=" + nts(ith * jth) + "\n";
			ith *= jth;
			jth = 0;
			dfs(a,b,c,d,step - 1);
			back;
			fuyuan;
			if (jth != 0 && ith % jth == 0)
			{
				s += nts(ith) + "/" + nts(jth) + "=" + nts(ith / jth) + "\n";
				ith /= jth;
				jth = 0;
				dfs(a,b,c,d,step - 1);
				back;
				fuyuan;
			}
		}
	}
}
signed main()
{
	cin >> a >> b >> c >> d;
	dfs(a,b,c,d,4);
	cout << "No answer!";
	return 0;
}

求助大佬!!!

2023/7/7 13:09
加载中...