B DEF
A + — + —— = 10
C GHI
这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
#include<cstdio>
#include<algorithm> //next_permutation全排列需要的库
using namespace std;
int main()
{
double p[9]; //1.因为有除法运算,精度方面用double处理,
//2.用int的话,也可以把式子变成乘法形式:A*C*GHI+B*GHI+DEF*C=10*C*GHI
int i;
int sum=0;
for(i=0;i<9;i++)
{
p[i]=i+1.0; //数组p[0~8]={1~9}
}
do
{
if(p[0]+p[1]/p[2]+(p[3]*100+p[4]*10+p[5])/(p[6]*100+p[7]*10+p[8])==10.0)
{
sum++;
}
}while(next_permutation(p,p+9)); //next_permutation(数组p,长度为9),即全排列数字1~9
printf("sum=%d\n",sum);
return 0;
}