Skip to content

Instantly share code, notes, and snippets.

@snoopspy
Created April 19, 2015 15:05
Show Gist options
  • Save snoopspy/a9e544c8092550794918 to your computer and use it in GitHub Desktop.
Save snoopspy/a9e544c8092550794918 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
using namespace std;
struct Num {
int n[9];
int cnt;
Num() {
for (int i = 0; i < 9; i++)
n[i] = i + 1;
cnt = 0;
}
bool check() {
int d1 = n[1] * 10 + n[2];
int d2 = n[4] * 10 + n[5];
int d3 = n[7] * 10 + n[8];
bool res = (n[0] * d2 * d3) + (n[3] * d1 * d3) + (n[6] * d1 * d2) == d1 * d2 * d3;
if (res) cnt++;
return res;
}
void write() {
for (int i = 0; i < 9; i++)
cout << n[i] << " ";
cout << endl;
}
bool next() {
return next_permutation(n, n + 9);
}
};
int main () {
Num num;
do {
if (num.check()) num.write();
} while (num.next());
cout << "cnt=" << num.cnt << endl;
return 0;
}
@snoopspy
Copy link
Author

5 3 4 7 6 8 9 1 2
5 3 4 9 1 2 7 6 8
7 6 8 5 3 4 9 1 2
7 6 8 9 1 2 5 3 4
9 1 2 5 3 4 7 6 8
9 1 2 7 6 8 5 3 4
cnt=6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment