rt
代码:
#include <bits/stdc++.h>
using pis = std::pair<int, std::string>;
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(nullptr);
std::vector<int> a(4, 0);
for(int i = 0; i < 4; ++i)
std::cin >> a[i];
auto calc = [&](int x, int y) -> std::vector<pis> {
std::vector<pis> res;
res.push_back({x+y, std::to_string(x) + "+" + std::to_string(y)
+ "=" + std::to_string(x+y) + "\n"});
if(x > y)
res.push_back({x-y, std::to_string(x) + "-" + std::to_string(y)
+ "=" + std::to_string(x-y) + "\n"});
res.push_back({x*y, std::to_string(x) + "*" + std::to_string(y)
+ "=" + std::to_string(x*y) + "\n"});
if(y > x)
res.push_back({y-x, std::to_string(y) + "-" + std::to_string(x)
+ "=" + std::to_string(y-x) + "\n"});
if(x >= y && x % y == 0)
res.push_back({x/y, std::to_string(x) + "/" + std::to_string(y)
+ "=" + std::to_string(x/y) + "\n"});
if(y >= x && y % x == 0)
res.push_back({y/x, std::to_string(y) + "/" + std::to_string(x)
+ "=" + std::to_string(y/x) + "\n"});
return res;
};
auto f = [&](auto &f) -> std::vector<pis> {
std::vector<pis> res;
if(a.size() == 1) {
res.push_back({a[0], ""});
return res;
}
for(int i = 0; i < a.size(); ++i) {
int tmp = a[i];
a.erase(a.begin()+i);
auto f_res = f(f);
a.insert(a.begin()+i, tmp);
for(auto x: f_res) {
auto calc_res = calc(x.first, tmp);
for(auto y: calc_res) {
res.push_back({y.first, x.second + y.second});
}
}
}
return res;
};
auto ans = f(f);
for(auto x: ans) {
if(x.first == 24) {
std::cout << x.second;
return 0;
}
}
auto g = [&]() -> std::vector<pis> {
std::vector<pis> res;
std::vector<int> tmp(2, 0);
for(int i = 0; i < 3; ++i) {
for(int j = i+1; j < 4; ++j) {
tmp[0] = a[i];
tmp[1] = a[j];
a.erase(a.begin()+j);
a.erase(a.begin()+i);
auto f_res = f(f);
a = tmp;
auto f2_res = f(f);
for(auto k: f_res) {
for(auto l: f2_res) {
auto calc_res = calc(k.first, l.first);
for(auto y: calc_res) {
res.push_back({y.first, k.second+l.second+y.second});
}
}
}
}
}
return res;
};
ans = g();
for(auto x: ans) {
if(x.first == 24) {
std::cout << x.second;
return 0;
}
}
std::cout << "No answer!";
}
WA 了三个点,用红名权限下载了 #6 的数据:
输入:
3 4 5 6
正确输出:
4-3=1
5-1=4
6*4=24
我的输出:
5-4=1
1+3=4
4*6=24
感觉也没有问题啊,是我的问题还是 Special Judge 不行?麻烦 dalao 帮忙看看...