标题:神奇算式
由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
比如:
210 x 6 = 1260
8 x 473 = 3784
27 x 81 = 2187
都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,一共有多少种满足要求的算式。
请填写该数字,通过浏览器提交答案,不要填写多余内容(例如:列出所有算式)。
答案:12
思路:枚举所有的四位数。
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
int main()
{
int a, b, c, d, m, n, i, j, flag, ans=0;
int num[10];
for (i = 1023; i <= 9876; i++)
{
a = i % 10;
b = i / 10 % 10;
c = i / 100 % 10;
d = i / 1000;
if (a == b || a == c || a == d || b == c || b == d || c == d)
continue;
for (j = 2; j < sqrt(i); j++)
{
memset(num, 0, sizeof(num));
num[a] = num[b] = num[c] = num[d] = 1;
if (i%j == 0)
{
flag = 1;
m = j;
n = i / j;
while (m)
{
if (num[m % 10] == 1)
num[m % 10]++;
else if (num[m % 10] == 0 || num[m % 10] == 2)
{
flag = 0;
break;
}
m /= 10;
}
if (!flag)
continue;
while (n)
{
if (num[n % 10] == 1)
num[n % 10]++;
else if (num[n % 10] == 0 || num[n % 10] == 2)
{
flag = 0;
break;
}
n /= 10;
}
if (flag)
{
cout << j << "*" << i / j << "=" << i << endl;
ans++;
}
}
}
}
cout << ans << endl;
return 0;
}