这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。
//先给出答案:29 //有两种思路:1、暴力枚举(这里讲这个) 2、全排列(下次补上) //注意啦:第一:浮点数注意事项。要不就使分子*1.0,然后abs(等式左边-等式右边)<1E-10;要不就左右两边通分,使分式运算化成整式运算。 //第二,关于“DEF”、"GHI"。这里DEF=D*100+E*10+F(而不是D*E*F,小心陷阱) #include<iostream> #include<cmath> using namespace std; int main() { int A, B, C, D, E, F, G, H, I; int count = 0; for (A = 1; A <= 9; A++) { for (B = 1; B <= 9; B++) { if (A == B)continue; for (C = 1; C <= 9; C++) { if (A == C || B == C)continue; for (D = 1; D <= 9; D++) { if (A == D || B == D || C == D)continue; for (E = 1; E <= 9; E++) { if (A == E || B == E || C == E || D == E)continue; for (F = 1; F <= 9; F++) { if (A == F || B == F || C == F || D == F || E == F)continue; for (G = 1; G <= 9; G++) { if (A == G || B == G || C == G || D == G || E == G || F == G)continue; for (H = 1; H <= 9; H++) { if (A == H || B == H || C == H || D == H || E == H || F == H || G == H)continue; for (I = 1; I <= 9; I++) { if (A == I || B == I || C == I || D == I || E == I || F == I || G == I || H == I)continue; int DEF = D * 100 + E * 10 + F; int GHI = G * 100 + H * 10 + I; if (abs(A + B*1.0 / C + DEF*1.0 / GHI-10)<1E-10) { count++; } } } } } } } } } } cout << count << endl; }