问题描述
B DEF
A + --- + ------- = 10
C GHI
(如果显示有问题,可以参见【图1.jpg】)
这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。
解决方案
思路
全排列
代码
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int ans = 0;
int buf[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
do {
int a = buf[0];
int b = buf[1];
int c = buf[2];
int def = buf[3] * 100 + buf[4] * 10 + buf[5];
int ghi = buf[6] * 100 + buf[7] * 10 + buf[8];
int b1 = b * ghi;
int c1 = c * ghi;
int def1 = def * c;
int ghi1 = c1;
int fenzi = b1 + def1;
int fenmu = c1;
//检查是符合整数
if (fenzi % fenmu != 0)
continue;
if (a + fenzi / fenmu == 10) {
// cout << a << "+" << b << "/" << c << "+" << def << "/" << ghi << endl;
ans++;
}
} while (next_permutation(buf, buf + 9));
cout << ans;
return 0;
}
输出
29