# 9数算式 观察如下的算式: ``` 9213 x 85674 = 789314562 ``` 左边的乘数和被乘数正好用到了1~9的所有数字,每个1次。 而乘积恰好也是用到了1~9的所有数字,并且每个1次。 请你借助计算机的强大计算能力,找出满足如上要求的9数算式一共有多少个? 注意: 1. 总数目包含题目给出的那个示例。 2. 乘数和被乘数交换后作为同一方案来看待。 ## aop ### before ```cpp #include using namespace std; int bei[10]; map mp; ``` ### after ```cpp ``` ## 答案 ```cpp int main() { int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int res = 0; do { for (int i = 1; i < 9; i++) { memset(bei, 0, sizeof(bei)); long long int ans, left = 0, right = 0, t = 0, x = 0, y = 0; for (int j = 0; j <= i; j++) { left = left * 10 + a[j]; } x = left; x = x * 10; for (int k = i + 1; k < 9; k++) { right = right * 10 + a[k]; x = x * 10 + a[k]; } y = right; y = y * 10; for (int j = 0; j <= i; j++) y = y * 10 + a[j]; ans = left * right; long long int ff = ans; while (ans > 0) { int x = ans % 10; ans = ans / 10; if (bei[x] == 0 && x != 0) { bei[x] = 1; t++; } } if (t == 9 && mp.count(x) == 0 && mp.count(y) == 0) { res++; mp[x] = 1; mp[y] = 1; } } } while (next_permutation(a, a + 9)); cout << res << endl; return 0; } ``` ## 选项 ### A ```cpp int main() { int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int res = 0; while (next_permutation(a, a + 9)) { for (int i = 1; i < 9; i++) { memset(bei, 0, sizeof(bei)); long long int ans, left = 0, right = 0, t = 0, x = 0, y = 0; for (int j = 0; j <= i; j++) { left = left * 10 + a[j]; } x = left; x = x * 10; for (int k = i + 1; k < 9; k++) { right = right * 10 + a[k]; } y = right; y = y * 10; for (int j = 0; j <= i; j++) y = y * 10 + a[j]; ans = left * right; long long int ff = ans; while (ans >= 0) { int x = ans % 10; ans = ans / 10; if (bei[x] == 0 && x != 0) { bei[x] = 1; t++; } } if (mp.count(x) == 0 && mp.count(y) == 0) { res++; mp[x] = 1; mp[y] = 1; } } }; cout << res << endl; return 0; } ``` ### B ```cpp int main() { int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int res = 0; do { for (int i = 1; i < 9; i++) { memset(bei, 0, sizeof(bei)); long long int ans, left = 0, right = 0, t = 0, x = 0, y = 0; for (int j = 0; j <= i; j++) { left = left * 10 + a[j]; } x = left; x = x * 10; for (int k = i + 1; k < 9; k++) { right = right * 10 + a[k]; x = x * 10 + a[k]; } y = right; y = y * 10; for (int j = 0; j <= i; j++) y = y * 10 + a[j]; long long int ff = ans; while (ans > 0) { int x = ans % 10; ans = ans / 10; if (bei[x] == 0 && x != 0) { bei[x] = 1; t++; } } if (t == 9 && mp.count(x) == 0 && mp.count(y) == 0) { res++; mp[x] = 1; mp[y] = 1; } } } while (next_permutation(a, a + 9)); cout << res << endl; return 0; } ``` ### C ```cpp int main() { int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int res = 0; while (next_permutation(a, a + 9)) { for (int i = 1; i < 9; i++) { memset(bei, 0, sizeof(bei)); long long int ans, left = 0, right = 0, t = 0, x = 0, y = 0; for (int j = 0; j <= i; j++) { left = left * 10 + a[j]; } x = left; x = x * 10; for (int k = i + 1; k < 9; k++) { right = right * 10 + a[k]; } y = right; y = y * 10; for (int j = 0; j <= i; j++) y = y * 10 + a[j]; ans = left * right; long long int ff = ans; while (ans > 0) { int x = ans % 10; ans = ans / 10; if (bei[x] == 0 && x != 0) { bei[x] = 1; t++; } } if (t == 9 && mp.count(x) == 0 && mp.count(y) == 0) { res++; mp[x] = 1; mp[y] = 1; } } }; cout << res << endl; return 0; } ```