问题描述
由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
比如:
210 x 6 = 1260
8 x 473 = 3784
27 x 81 = 2187
都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,一共有多少种满足要求的算式。
请填写该数字,通过浏览器提交答案,不要填写多余内容(例如:列出所有算式)。
解题方案
思路
填空题直接暴力
代码
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
bool check(int a, int b) {
stringstream buf1;
string str1;
buf1 << a;
buf1 >> str1;
stringstream buf2;
string str2;
buf2 << b;
buf2 >> str2;
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
return str1 == str2;
}
int main() {
int ans = 0;
for (int i = 1; i <= 9; ++i) {
for (int j = 0; j <= 9; ++j) {
if (i != j)
for (int k = 0; k <= 9; ++k) {
if (i != k && j != k)
for (int l = 0; l <= 9; ++l) {
if (i == l || j == l || k == l)
continue;
{
int n1 = i * 100 + j * 10 + k;
int n2 = l;
int ret = n1 * n2;
if (n2 != 0)
if (check(n1 * 10 + n2, ret)) {
ans++;
// cout << n1 << "*" << n2 << "=" << ret << endl;
}
}
{
int n1 = i * 10 + j;
int n2 = l * 10 + k;
int ret = n1 * n2;
if (n2 > 10 && n1 > n2)
if (check(n1 * 100 + n2, ret)) {
ans++;
// cout << n1 << "*" << n2 << "=" << ret << endl;
}
}
}
}
}
}
cout << ans << endl;
return 0;
}
输出
12