神奇算式
由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
比如:
210 x 6 = 1260
8 x 473 = 3784
27 x 81 = 2187
都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,一共有多少种满足要求的算式。
#include <cstdio>
#include <algorithm>
#include<iostream>
using namespace std;
int ans;
int main()
{
for (int i = 1023; i < 9876; i ++)
{
int a = i / 1000;
int b = i / 100 % 10;
int c = i / 10 % 10;
int d = i % 10;
if(a == b || a == c || a == d || b == c || b == d || c == d) continue;
int s[4] = {a, b, c, d};
while(next_permutation(s, s + 4))//STL排列组合
{//一共有两种可能 2位x2位 和 1位x3位
//然后2位x2位左右可能重复
//1位x3位和3位x1位可能重复
//或者“人眼去重复”
if(s[0] * (s[1]*100 + s[2]*10 + s[3]) == i) {ans++;printf("%d * %d%d%d==%d\n", s[0], s[1], s[2], s[3],i);}
if(s[3] * (s[0]*100 + s[1]*10 + s[2]) == i) {ans++;printf("%d * %d%d%d==%d\n", s[3], s[0], s[1], s[2],i);}
if((s[0]*10 + s[1]) * (s[2]*10 + s[3]) == i) {ans++;printf("%d%d * %d%d==%d\n", s[0], s[1], s[2], s[3],i);}
}
}
cout << "ans:" << ans/2 << endl;//取重
return 0;
}