Как упростить код Ребус: BMW+ZAZ=MOTO
}
for (int M = 1; M <= 9; ++M) {
if (M == A || M == B) {
continue;
}
for (int O = 0; O <= 9; ++O) {
if (O == A || O == B || O == M) {
continue;
}
for (int T = 0; T <= 9; ++T) {
if (T == A || T == B || T == M || T == O) {
continue;
}
for (int W = 0; W <= 9; ++W) {
if (W == A || W == B || W == M || W == O || W == T) {
continue;
}
for (int Z = 0; Z <= 9; ++Z) {
if (Z == A || Z == B || Z == M || Z == O || Z == T) {
continue;
}
int BMV = B * 100 * M * 10 + W;
int ZAZ = Z * 100 * A * 10 + Z;
int MOTO = M * 1000 + O * 100 + T * 10 + O;
if (BMV + ZAZ == MOTO) {
cout << B << M << W << " + " << Z << A << Z << " - " << M << O << T << O << endl;
}
}
}
}
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Что-то типа
#include <iostream>
#include <algorithm>
#include <functional>
template<class RandIt, class Compare>
bool next_k_permutation(RandIt first, RandIt mid, RandIt last, Compare comp)
{
std::sort(mid, last, std::not_fn(comp));
return std::next_permutation(first, last, comp);
}
template<class RandIt>
bool next_k_permutation(RandIt first, RandIt mid, RandIt last)
{
return next_k_permutation(first, mid, last,std::less<>());
}
int main()
{
// B M W Z A O T
int v[] = {0,1,2,3,4,5,6,7,8,9};
do {
if (((v[0]+v[3]-v[5])*10+v[1]+v[4]-v[6])*10+v[2]+v[3]-v[5] == 1000*v[1])
std::cout
<< v[0] << v[1] << v[2] << "+"
<< v[3] << v[4] << v[3] << "="
<< v[1] << v[5] << v[6] << v[5] << "\n";
} while(next_k_permutation(v,v+7,v+10));
}