博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
24点小游戏算法
阅读量:4230 次
发布时间:2019-05-26

本文共 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 即可判断运算过程。

附上源码供参考

#include
using 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/

你可能感兴趣的文章
Web Site Measurement Hacks
查看>>
The Best Damn Windows Server 2003 Book Period
查看>>
Cleaning Windows XP For Dummies
查看>>
The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
查看>>
Python in a Nutshell
查看>>
Microsoft Visual C++ .NET Professional Projects
查看>>
Excel 2007 Advanced Report Development
查看>>
Security Metrics: Replacing Fear, Uncertainty, and Doubt
查看>>
Accelerating Process Improvement Using Agile Techniques
查看>>
The New Language of Business: SOA & Web 2.0
查看>>
Programming Mobile Devices: An Introduction for Practitioners
查看>>
Designing for Networked Communications: Strategies and Development
查看>>
Building a Monitoring Infrastructure with Nagios
查看>>
Illustrated C# 2005
查看>>
Pro ASP.NET 2.0 E-Commerce in C# 2005
查看>>
Thinking Animation: Bridging the Gap Between 2D and CG
查看>>
Ajax in Practice
查看>>
Flash Animation for Teens
查看>>
The Oracle Hacker's Handbook: Hacking and Defending Oracle
查看>>
Microsoft Windows PowerShell: TFM
查看>>