#include"conio.h"#include"stdlib.h"#include"time.h"#include"math.h"#include"string."> #include"conio.h"#include"stdlib.h"#include"time.h"#include"math.h"#include"string." />
用C语言实现24点的算法程序

用C语言实现24点的算法程序

ID:39267032

大小:39.00 KB

页数:9页

时间:2019-06-29

用C语言实现24点的算法程序_第1页
用C语言实现24点的算法程序_第2页
用C语言实现24点的算法程序_第3页
用C语言实现24点的算法程序_第4页
用C语言实现24点的算法程序_第5页
资源描述:

《用C语言实现24点的算法程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、//#include"stdafx.h"#include#include"conio.h"#include"stdlib.h"#include"time.h"#include"math.h"#include"string.h"#include"iostream.h"/*从一副扑克牌中,任取4张。2-10按其点数计算(为了表示方便10用T表示),J,Q,K,A统一按1计算要求通过加减乘除四则运算得到数字24。本程序可以随机抽取纸牌,并用试探法求解。*/ints[21][4]={-1};intsum=0;voidGivePuzzle(char*buf)

2、{charcard[]={'A','2','3','4','5','6','7','8','9','T','J','Q','K'};for(inti=0;i<4;i++){buf[i]=card[rand()%13];}}voidSetPuzzle(char*buf){scanf("%c%c%c%c",&buf[0],&buf[1],&buf[2],&buf[3]);}voidshuffle(char*buf){for(inti=0;i<5;i++){intk=rand()%4;chart=buf[k];buf[k]=buf[0];buf[0]=t;}}intGet

3、CardValue(intc){if(c=='T')return10;if(c>='0'&&c<='9')returnc-'0';return1;}charGetOper(intn){switch(n){case0:return'+';case1:return'-';case2:return'*';case3:return'/';}return'';}doubleMyCalcu(doubleop1,doubleop2,intoper){switch(oper){case0:returnop1+op2;case1:returnop1-op2;case2:returno

4、p1*op2;case3:if(fabs(op2)>0.0001)returnop1/op2;elsereturn100000;}return0;}voidMakeAnswer(char*answer,inttype,char*question,int*oper){charp[4][3];for(inti=0;i<4;i++){if(question[i]=='T')strcpy(p[i],"10");elsesprintf(p[i],"%c",question[i]);}switch(type){case0:sprintf(answer,"%s%c(%s%c(%s

5、%c%s))",p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);break;case1:sprintf(answer,"%s%c((%s%c%s)%c%s)",p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);break;case2:sprintf(answer,"(%s%c%s)%c(%s%c%s)",p[0],GetOper(oper[0]),p[1],GetOper(ope

6、r[1]),p[2],GetOper(oper[2]),p[3]);break;case3:sprintf(answer,"((%s%c%s)%c%s)%c%s",p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);break;case4:sprintf(answer,"(%s%c(%s%c%s))%c%s",p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);break;}}bool

7、TestResolve(char*question,int*oper,char*answer){//等待考生完成inttype[5]={0,1,2,3,4};//计算类型doublep[4];doublesum=0;for(inti=0;i<4;i++)//循环取得点数{p[i]=GetCardValue(int(question[i]));}for(i=0;i<5;i++){MakeAnswer(answer,type[i],question,oper);//获取可能的答案//printf("Doing:%s",answer);switch(type[i]

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。