代码如下:
#include <iostream>
using namespace std;
const int N = 15;
bool st[N];
double a[N];int cnt;
void dfs(int u) {if (u == 10) {if (a[1] + a[2] / a[3] + (a[4] * 100 + a[5] * 10 + a[6]) / (a[7] * 100 + a[8] * 10 + a[9]) == 10) {cnt++;}}for (int i = 1; i <= 9; i++) {if (!st[i]) {st[i] = true;a[u] = i;dfs(u + 1);st[i] = false;a[u] = 0;}}
}int main() {dfs(1);cout << cnt << endl;return 0;
}
next_permutation()写法代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
double a[] = {0,1,2,3,4,5,6,7,8,9};bool check()
{if (a[1] + a[2] / a[3]+ (a[4] * 100 + a[5] * 10 + a[6]) / (a[7] * 100 + a[8] * 10 + a[9]) == 10)return true;return false;
}
int ans;
int main()
{do{if (check())ans++;}while(next_permutation(a+1,a+10));cout<<ans<<endl;return 0;
}