博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
24点游戏 程序(二)
阅读量:4565 次
发布时间:2019-06-08

本文共 3198 字,大约阅读时间需要 10 分钟。

前面既然确定了后缀表达式的序列。

那就可以开始遍历了。

#include <iostream> #include <stack> #include <algorithm> #include <string> #include <cmath> #include <time.h> using namespace std; struct TOperData { int operType; double data; }; //检查计算的参数是否合法,也可以用于过滤一些重复的表达式 bool checkjisuan(double a, double b, int op) { //处理除以0的情况 if (op == '/' && b < 1e-6 && b > -1e-6) return false; return true; } //求值 double jisuan(double a, double b, int op) { switch(op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return -1; } } //计算表达式的值 double process(TOperData data[]) { int len = 7; stack<TOperData> suffix; for (int i = 0; i < len; i++) { if (data[i].operType == 0) { suffix.push(data[i]); } else if (data[i].operType > 0) { if (suffix.empty()) return false; if (suffix.top().operType == 0) { double a = suffix.top().data; suffix.pop(); if (!suffix.empty() && suffix.top().operType == 0) { double b = suffix.top().data; suffix.pop(); if (!checkjisuan(b,a,data[i].operType)) return -1; double c = jisuan(b,a,data[i].operType); TOperData opdata; opdata.operType = 0; opdata.data = c; suffix.push(opdata); } else { return -1; } } else { return -1; } } } if (suffix.empty()) return -1; if (suffix.top().operType == 0) { double r = suffix.top().data; suffix.pop(); if (suffix.empty()) return r; } return -1; } //打印成功的结果 void printResult(TOperData computeData[]) { for (int i = 0; i < 7; i++) { if (computeData[i].operType == 0) { cout<<computeData[i].data<<" "; } else { cout<<(char)computeData[i].operType<<" "; } } cout<<endl; } int start(double data[]) { int shunxu[][7] = { 1,1,1,1,2,2,2 , 1,1,1,2,1,2,2 , 1,1,1,2,2,1,2 , 1,1,2,1,1,2,2 , 1,1,2,1,2,1,2 }; int operAll[] = {'+','-','*','/'}; TOperData computeData[7]; double copydata[4]; copydata[0] = data[0]; copydata[1] = data[1]; copydata[2] = data[2]; copydata[3] = data[3]; //5个后缀表达式遍历 for (int m = 0; m < 5; m++) { do { int oper[3]; for(int n = 0; n < 4; n++) { oper[0] = operAll[n]; for(int nn = 0; nn < 4; nn++) { oper[1] = operAll[nn]; for(int nnn = 0; nnn < 4; nnn++) { oper[2] = operAll[nnn]; int j = 0; int k = 0; for (int i = 0; i < 7; i++) { if (shunxu[m][i] == 1) { computeData[i].operType = 0; computeData[i].data = data[j++]; } else if (shunxu[m][i] == 2) { computeData[i].operType = oper[k++]; } } double r = process(computeData); if (r-24 > -1e-6 && r-24 < 1e-6) { cout<<copydata[0]<<" "<<copydata[1]<<" "<<copydata[2]<<" "<<copydata[3]<<","; printResult(computeData); //return 1;如果只要求一个结果,这个地方就可以返回了 } } } } } while ( next_permutation (data,data+4) ); } return 0; } int main(void) { long beginTime =clock();//获得开始时间,单位为毫秒 double data[4]; for (int i = 1; i< 14; i++) { data[0] = i; for (int ii = i; ii< 14; ii++) { data[1] = ii; for (int iii = ii; iii< 14; iii++) { data[2] = iii; for (int iiii = iii; iiii< 14; iiii++) { data[3] = iiii; double copydata[4]; copydata[0] = data[0]; copydata[1] = data[1]; copydata[2] = data[2]; copydata[3] = data[3]; start(copydata); } } } } long endTime=clock();//获得结束时间 cout<<"time:"<<endTime-beginTime<<endl; return 0; }

这样执行得到50000个结果,耗时30s左右

仔细看这些结果,可以发现有一些重复的。

接下来要简单消除一下重复,以及为了结果更直观,把后缀表达式改成中缀表达式。

转载于:https://www.cnblogs.com/marryZhan/archive/2012/02/26/2497559.html

你可能感兴趣的文章
A - Mike and palindrome
查看>>
DOTween教程
查看>>
java web中java和python混合使用
查看>>
创建学员类和教员类
查看>>
Cookie和Session的作用和工作原理
查看>>
字符串操作
查看>>
Visual Studio中改变environment 的布局和显示风格
查看>>
2016-XCTF Final-Richman
查看>>
文件下载
查看>>
extjs grid renderer用法
查看>>
vue 如何在循环中绑定v-model
查看>>
shell脚本
查看>>
[代码笔记]JS保持函数单一职责,灵活组合
查看>>
cmd 重定向
查看>>
【IOS开发】如何画1像素的线
查看>>
【计算机视觉】双目测距(五)--匹配算法对比
查看>>
KMP模板
查看>>
luogu 1314 聪明的质检员
查看>>
[转载]求职者防骗必读!楼主亲身经历告诉你岗前培训多么不靠谱而且违法!
查看>>
Hibernate内存溢出分析一例
查看>>