法一:
代码如下:
#include <iostream>
using namespace std;
const int N = 15;
bool st[N];
int a[N];void dfs(int u) {if (u == 11) {if ((((a[1] * 1000 + a[2] * 100 + a[3] * 10 + a[4]) - (a[5] * 1000 + a[6] * 100 + a[7] * 10 + a[8])) * (a[9] *10 + a[10])) == 900) {for (int i = 1; i <= 10; i++) {cout << a[i] << " ";}cout << endl;return ;}}if (u == 1) {//第一个位置不能为0for (int i = 1; i <= 9; i++) {if (!st[i]) {st[i] = true;a[u] = i;dfs(u + 1);a[u] = -1;st[i] = false;}}} else if (u == 9) {//第9个位置不能为0for (int i = 1; i <= 9; i++) {if (!st[i]) {st[i] = true;a[u] = i;dfs(u + 1);a[u] = -1;st[i] = false;}}}elsefor (int i = 0; i <= 9; i++) {if (!st[i]) {st[i] = true;a[u] = i;dfs(u + 1);a[u] = -1;st[i] = false;}}
}int main() {memset(a, -1, sizeof(a));dfs(1);return 0;
}
法二:
#include <iostream>
#include <algorithm>
using namespace std;int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};void check() {if (((a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3]) - (a[4] * 1000 + a[5] * 100 + a[6] * 10 + a[7])) *(a[8] * 10 + a[9]) == 900&& a[0] != 0 && a[4] != 0 && a[8] != 0 ) {cout << a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3] << " " << a[4] * 1000 + a[5] * 100 + a[6] * 10 + a[7] << " " <<a[8] * 10 + a[9] << endl;}
}int main() {do {check();} while (next_permutation(a, a + 10));return 0;
}