问题描述
小明的作业本上有道思考题: 看下面的算式:(□□□□-□□□□)*□□=900
其中的小方块代表 0 ~ 9 的数字,这 10 个方块刚好包含了 0 ~ 9 中的所有数字。
小明经过几天的努力,终于做出了答案!如下: (5012-4987)*36=900
用计算机搜索后,发现还有另外一个解,本题的任务就是:请你算出这另外的一个解。
注意:0 不能作为某个数字的首位。
答案提交
注意:提交的格式需要与示例严格一致;不要填写任何多余的内容,比如说明文字。
- 括号及运算符号不要用中文输入法;
- 整个算式中不能包含空格。
答案:(6048-5973)*12
题解
全排列:
#include <algorithm>
#include <cstdio>
using namespace std;
int s[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int main()
{
do
{
if(!s[0] || !s[4] || !s[8]) continue;
int a = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];
int b = s[4] * 1000 + s[5] * 100 + s[6] * 10 + s[7];
int c = s[8] * 10 + s[9];
if((a - b) * c == 900) printf("(%d-%d)*%d=900\n", a, b, c);
}while(next_permutation(s, s + 10));
return 0;
}