本文共 3528 字,大约阅读时间需要 11 分钟。
要求:1-9任意四个数字,利用加减乘除括号五种运算使最终结果等于24;如果能达到要求,打印所有方法(同样的加减乘除,括号不同位置算两种不同的方法);若不能达到要求,打印“不能计算达到24”。
思考过程:四个数字,三步运算,二个括号,而这两个括号的位置有五种不同的情况。也就是说,不论运算符号是什么,利用括号都可以有五种不同的运算顺序。
(1)((x,y),z),w (2)(x,y),(z,w) (3)(x,(y,z),w) (4)x,((y,z),w) (5)x,((y,(z,w))因此,我的代码实现过程是,由于三步运算,就利用三重循环。每一步循环定义了五个局部变量来记录五种情况各自的第一步的暂时结果,在最后一步进行判断结果是否为24(在第三步循环之内)。如果是,则需要打印计算过程。
for (int i = 1; i < 5; i++) { suma = cal(i, x, y); sumb = cal(i, x, y); sumc = cal(i, y, z); sumd = cal(i, y, z); sume = cal(i, z, w); for (int j = 1; j < 5; j++) { suma1 = cal(j, suma, z); sumb1 = cal(j, z, w); sumc1 = cal(j, x, sumc); sumd1 = cal(j, sumd, w); sume1 = cal(j, y, sume); for (int k = 1; k < 5; k++) { suma2 = cal(k, suma1, w); sumb2 = cal(k, sumb, sumb1); sumc2 = cal(k, sumc1, w); sumd2 = cal(k, x, sumd1); sume2 = cal(k, x, sume1); //加上判断 //其中cal(int n,int x,int y)的功能是根据n的值决定对x,y做什么运算,返回结果。 } } }
有人可能会问,是不是需要一个容器来记录前面每一步过程做的运算?
其实是不需要的。三步循环,也就是三个运算符(不考虑括号,因为括号是根据五种运算顺序事先就已经决定好了的。),设循环变量分别为i,j,k,在做最内层循环的时候,是有对应的i,j的,只要利用一个函数将i,j,k与运算符号相对应起来就可以了!也就是一个函数参数为0-4(1-5也可),函数体为switch…case或者if…else if…去对应运算符号即可。这样在第三层循环内判断完之后打印时,直接利用i,j,k 即可判断运算过程。
附上源码供参考
#includeusing namespace std;double cal(int n, double x, double y){ double f; if (n == 1) f = x + y; else if (n == 2) f = x*y; else if (n == 3) f = x / y; else if (n == 4) f = x - y; return f;}void print(int n){ if (n == 1) cout << '+'; else if (n == 2) cout << '*'; else if (n == 3) cout << '/'; else if (n == 4) cout << '-';}int main(){ double x, y, z, w; int leap = 0; while (1) { cout << "please input four integer between 1 to 9 : "; cin >> x >> y >> z >> w; if (cin.fail()) { cin.clear(); cin.ignore(1024, '\n'); } else if (x < 10 && x>0 && y < 10 && y>0 && z < 10 && z>0 && w < 10 && w>0) break; } for (int i = 1; i < 5; i++) { double suma = 0, suma1 = 0, suma2 = 0; double sumb = 0, sumb1 = 0, sumb2 = 0; double sumc = 0, sumc1 = 0, sumc2 = 0; double sumd = 0, sumd1 = 0, sumd2 = 0; double sume = 0, sume1 = 0, sume2 = 0; suma = cal(i, x, y); sumb = cal(i, x, y); sumc = cal(i, y, z); sumd = cal(i, y, z); sume = cal(i, z, w); for (int j = 1; j < 5; j++) { suma1 = cal(j, suma, z); sumb1 = cal(j, z, w); sumc1 = cal(j, x, sumc); sumd1 = cal(j, sumd, w); sume1 = cal(j, y, sume); for (int k = 1; k < 5; k++) { suma2 = cal(k, suma1, w); sumb2 = cal(k, sumb, sumb1); sumc2 = cal(k, sumc1, w); sumd2 = cal(k, x, sumd1); sume2 = cal(k, x, sume1); if (suma2 == 24) { leap = 1; cout << "((" << x; print(i); cout << y << ")"; print(j); cout << z << ")"; print(k); cout << w << "=24" << endl; } if (sumb2 == 24) { leap = 1; cout << "(" << x; print(i); cout << y << ")"; print(k); cout << "(" << z; print(j); cout << w << ")=24" << endl; } if (sumc2 == 24) { leap = 1; cout << "(" << x; print(j); cout << "(" << y ; print(i); cout << z << "))"; print(k); cout << w << "=24" << endl; } if (sumd2 == 24) { leap = 1; cout << x; print(k); cout << "((" << y ; print(i); cout << z << ")"; print(j); cout << w << ")=24" << endl; } if (sume2 == 24) { leap = 1; cout << x; print(k); cout << "(" << y ; print(j); cout << "(" << z; print(i); cout << w << "))=24" << endl; } } } } if (leap == 0) cout << "无解" << endl; return 0;}
转载地址:http://ngiqi.baihongyu.com/