本地可过,洛谷评测全 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;
}
求助大佬!!!