1、标题:填算式
请看下面的算式:
(ABCD - EFGH) * XY = 900
每个字母代表一个0~9的数字,不同字母代表不同数字,首位不能为0。
比如,(5012 - 4987) * 36 就是一个解。
请找到另一个解,并提交该解中 ABCD 所代表的整数。
代码提交
#include <stdio.h>
int a[10] = {0}, book[10] = {0};
int first = 0, second = 0, third = 0;
void dfs(int step) {
int i;
if (step == 10) {
first = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
second = a[4] * 1000 + a[5] * 100 + a[6] * 10 + a[7];
third = a[8] * 10 + a[9];
if ((first - second ) * third == 900 && first >= 1000
&& second >= 1000 && third >= 10) {
printf("(%d - %d) * %d = 900\n", first, second, third);
return;
}
}
for (int i = 0; i <= 9; i++) {
if (book[i] == 0) {
a[step] = i;
book[i] = 1;
dfs(step + 1);
book[i] = 0;
}
}
return;
}
int main() {
dfs(0);
return 0;
}