利用c++ 做二十四点游戏。请高手们帮忙!谢谢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用c++ 做二十四点游戏。请高手们帮忙!谢谢相关的知识,希望对你有一定的参考价值。

利用c++写一游戏。让用户输入4个牌的点数,电脑计算24点,并且用直
观的式子表示计算过程。
a) 输入的可以是A,2,3,4,5,6,7,8,9,10,J,Q,K,输出的表达式亦用该形式表示,
即K不表示为13.
b) 游戏可以重复进行,直到用户输入的数中含有负数则退出。
c) 要有一定的人性化的界面提示。
d) 没有24计算方案的时候要通知用户,并要求重新计算

越基础的形式,就越好!
每行都有注释更好!

不太明白问题的意思的人,
请参考8.关于二十四点游戏的编程思路与基本算法:
http://www.hlib.cn/article/program/842.asp
如果有合适的答案
会有50分的 附加分

#define N 20
#define COL 100
#define ROW 40
#include "stdio.h"
#include "time.h" /*系统时间函数*/
#include "graphics.h" /*图形函数*/
#include "alloc.h"/*动态地址分配函数*/
#include "stdlib.h" /*库函数*/
#include "string.h" /*字符串函数*/
#include "ctype.h" /*字符操作函数*/
char p[4][13]=
'A','2','3','4','5','6','7','8','9','0','J','Q','K',/*扑克牌,10用0来表示*/
'A','2','3','4','5','6','7','8','9','0','J','Q','K',
'A','2','3','4','5','6','7','8','9','0','J','Q','K',
'A','2','3','4','5','6','7','8','9','0','J','Q','K';
typedef struct node

int data;
struct node *link;
STACK1; /*栈1*/
typedef struct node2

char data;
struct node2 *link;
STACK2; /*栈2*/
void init(void);/*图形驱动*/
void close(void);/*图形关闭*/
void play(void);/*发牌的具体过程*/
void rand1(int j);/*随机发牌函数*/
void change(char *e,char *a); /*中缀变后缀函数*/
int computer(char *s); /*后缀表达式计算函数*/
STACK1 *initstack1(STACK1 *top); /*栈1初始化*/
STACK1 *push(STACK1 *top,int x); /*栈1入栈运算*/
STACK1 *pop(STACK1 *top); /*栈1删除栈顶元素*/
int topx(STACK1 *top); /*栈1读栈顶元素*/
STACK1 *ptop(STACK1 *top,int *x); /*栈1读出栈顶元素值并删除栈顶元素*/
int empty(STACK1 *top); /*判栈1是否为空函数*/
STACK2 *initstack2(STACK2 *top); /*栈2初始化*/
STACK2 *push2(STACK2 *top,char x); /*栈2入栈运算*/
STACK2 *pop2(STACK2 *top); /*栈2删除栈顶元素*/
char topx2(STACK2 *top); /*栈2读栈顶元素*/
STACK2 *ptop2(STACK2 *top,char *x); /*栈2读出栈顶元素值并删除栈顶元素*/
int empty2(STACK2 *top); /*判栈2是否为空函数*
int text1(char *s) ; /*显示文本*/
main()

char s[N],s1[N],ch;
int i,result;
int gdriver, gmode;
clrscr(); /*清屏*/
init(); /*初始化函数*/
while(1)

setbkcolor(BLACK); /*设置背景颜色*/
cleardevice();/*清屏*/
play(); /*发牌*/
gotoxy(1,15); /*移动光标*/
printf("--------------------Note-------------------\n");
printf(" Please enter express accroding to above four number\n"); /*提示信息*/
printf(" Format as follows:2.*(5.+7.)\n");/*提示输入字符串格式*/
printf(" ----------------------------------------------\n");
scanf("%s%c",s1,&ch); /*输入字符串压回车键*/
change(s1,s); /*调用change函数将中缀表达式s1转换为后缀表达式s*/
result=computer(s); /*计算后缀表达式的值,返回结果result */
if(result==24) /*如果结果等于24*/
text1("very good"); /*调用函数text1显示字符串"very good"*/
else
text1("wrong!!!");/*否则函数text1显示字符串"wrong!!!"*/
printf("Continue (y/n)?\n"); /*提示信息,是否继续*/
scanf("%c",&ch); /*输入一字符*/
if(ch=='n'||ch=='N') /*如果该字符等于n或N*/
break; /*跳出循环,程序结束*/
/*否则,开始下一轮循环*/
close();
return; /*返回*/

void rand1(int j)/*随机发牌函数*/

int kind,num;
char str[3],n;
randomize();
while(1)/*循环直到有牌发*/

kind=random(4); /*花色随机数*/
num=random(13); /*大小随机数*/
if(p[kind][num]!=-1) /*该数未取过*/

n=p[kind][num]; /*取相应位置的扑克牌数*/
p[kind][num]=-1; /*牌发好以后相应位置的元素置-1*/
break;


switch(kind)/*花式的判断*/

case 0:setcolor(RED);sprintf(str,"%c",3);break; /*红桃*/
case 1:setcolor(BLACK);sprintf(str,"%c",3);break; /*黑桃*/
case 2:setcolor(RED);sprintf(str,"%c",4);break; /*方片*/
case 3:setcolor(BLACK);sprintf(str,"%c",5);break; /*草花*/

settextstyle(0,0,2);
outtextxy(COL+j*100-30,ROW+100-46,str);/*显示左上角花色*/
outtextxy(COL+j*100+16,ROW+100+32,str); /*显示右下角花色*/
if(n!='0')/*输出其他牌*/

settextstyle(0,0,3);
sprintf(str,"%c",n);
outtextxy(COL+j*100-5,ROW+100-5,str);/*显示牌的大小*/

else/*输出10的时候*/

sprintf(str,"%d",10);
outtextxy(COL+j*100-6,ROW+100-5,str);


void play(void)/*发牌的具体过程*/

int j;
for(j=0;j<4;j++)

bar(COL+j*100-35,ROW+100-50,COL+j*100+35,ROW+1*100+50);/*画空牌*/
setcolor(BLUE);
rectangle(COL+j*100-32,ROW+100-48,COL+j*100+32,ROW+100+48); /*画矩形框*/
rand1(j); /*随机取牌*/
delay(10000); /*延时显示*/


void init(void)/*图形驱动*/

int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();

void close(void)/*图形关闭*/

closegraph();

void change(char *e,char *a) /*中缀字符串e转后缀字符串a函数*/

STACK2 *top=NULL; /* 定义栈顶指针*/
int i,j;char w;
i=0;
j=0;
while(e[i]!='\0') /*当字符串没有结束时*/

if(isdigit(e[i])) /*如果字符是数字*/

do
a[j]=e[i]; /*将数字原样拷贝到数组a中*/
i++; /*e数组的下标加1*/
j++; /*a数组的下标加1*/
while(e[i]!='.'); /*直到字符为数字结束符“.”为止*/
a[j]='.';j++; /*将数字结束符“.”拷贝到a数组依然保持结束标记*/

if(e[i]=='(') /*如果字符是“(”时*/
top=push2(top,e[i]); /*将其压入堆栈*/
if(e[i]==')') /*如果字符是“)”时*/

top=ptop2(top,&w); /*取出栈顶元素,并从栈顶删除该元素*/
while(w!='(') /*如果字符不是“(”时反复循环*/

a[j]=w; /*将栈顶元素存入a数组*/
j++; /*下标加1*/
top=ptop2(top,&w) ; /*取出栈顶元素,并从栈顶删除该元素*/


if(e[i]=='+'||e[i]=='-') /*如果字符是加或减号时*/

if(!empty2(top)) /*如栈不为空*/

w=topx2(top);
while(w!='(') /*当栈顶元素不是“(”时反复循环*/

a[j]=w;
j++; /*将栈顶元素存入表达式a中,a的下标加1*/
top=pop2(top); /*删除栈顶元素*/
if(empty2(top)) /*如果栈为空*/
break; /*跳出循环*/
else
w=topx2(top); /*否则读栈顶元素*/


top=push2(top,e[i]); /*将当前e的字符元素压入堆栈*/

if(e[i]=='*'||e[i]=='/') /*如果字符是乘或除号时*/

if(!empty2(top)) /*如栈不为空*/

w=topx2(top); /*读栈顶元素存入w*/
while(w=='*'||w=='/')/*当栈顶元素是乘或除时反复循环*/

a[j]=w;
j++; /*将栈顶元素存入字符串a中,a的下标加1*/
top=pop2(top); /*删除栈顶元素*/
if(empty2(top)) /*如果栈为空*/
break; /*跳出循环*/
else
w=topx2(top); /*否则读栈顶元素*/


top=push2(top,e[i]); /*将当前e字符元素压入堆栈*/

i++; /*e的下标加1*/

while(!empty2(top)) /*当不为空时反复循环*/
top=ptop2(top,&a[j++]); /*将栈顶元素存入数组a中*/
a[j]='\0'; /*将字符串结束标记写入最后一个数组元素中构成字符串*/

int computer(char *s) /* 计算函数*/

STACK1 *top=NULL;
int i,k,num1,num2,result;
i=0;
while(s[i]!='\0') /*当字符串没有结束时作以下处理*/

if(isdigit(s[i])) /*判字符是否为数字*/

k=0; /*k初值为0*/
do
k=10*k+s[i]-'0'; /*将字符连接为十进制数字*/
i++; /*i加1*/
while(s[i]!='.'); /*当字符不为‘.’时重复循环*/
top=push(top,k); /*将生成的数字压入堆栈*/

if(s[i]=='+') /*如果为'+'号*/

top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num2+num1; /*将num1和num2相加存入result中*/
top=push(top,result); /*将result压入堆栈*/

if(s[i]=='-') /*如果为'-'号*/

top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num1-num2; /*将num1减去num2结果存入result中*/
top=push(top,result); /*将result压入堆栈*/

if(s[i]=='*') /*如果为'*'号*/

top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num1*num2; /*将num1与num2相乘结果存入result中*/
top=push(top,result); /*将result压入堆栈*/

if(s[i]=='/') /*如果为'/'号*/

top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num1/num2; /*将num1除num2结果存入result中*
top=push(top,result); /*将result压入堆栈*/

i++; /*i加1*/

top=ptop(top,&result); /*最后栈顶元素的值为计算的结果*/
return result; /*返回结果*/

STACK1 *initstack1(STACK1 *top) /*初始化*/

top=NULL; /*栈顶指针置为空*/
return top; /*返回栈顶指针*/

STACK1 *push(STACK1 *top,int x) /*入栈函数*/

STACK1 *p; /*临时指针类型为STACK1*/
p=(STACK1 *)malloc(sizeof(STACK1)); /*申请STACK1大小的空间*/
if(p==NULL) /*如果p为空*/

printf("memory is overflow\n!!"); /*显示内存溢出*/
exit(0); /*退出*/

p->data=x; /*保存值x到新空间*/
p->link=top; /*新结点的后继为当前栈顶指针*/
top=p; /*新的栈顶指针为新插入的结点*/
return top; /*返回栈顶指针*/

STACK1 *pop(STACK1 *top) /*出栈*/

STACK1 *q; /*定义临时变量*/
q=top; /*保存当前栈顶指针*/
top=top->link; /*栈顶指针后移*/
free(q); /*释放q*/
return top; /*返回栈顶指针*/

int topx(STACK1 *top) /*读栈顶元素*/

if(top==NULL) /*栈是否为空*/

printf("Stack is null\n"); /*显示栈为空信息*/
return 0; /*返回整数0*/

return top->data; /*返回栈顶元素*/

STACK1 *ptop(STACK1 *top,int *x) /*取栈顶元素,并删除栈顶元素*/

*x=topx(top); /*读栈顶元素*/
top=pop(top); /*删除栈顶元素*/
return top; /*返回栈顶指针*/

int empty(STACK1 *top) /*判栈是否为空*/

if(top==NULL) /*如果为空*/
return 1; /*返回1*/
else
return 0; /*否则返回0*/

STACK2 *initstack2(STACK2 *top) /*初始化*/

top=NULL; /*栈顶指针置为空*/
return top; /*返回栈顶指针*/

STACK2 *push2(STACK2 *top,char x) /*入栈函数*/

STACK2 *p; /*临时指针类型为STACK2*/
p=(STACK2 *)malloc(sizeof(STACK2)); /*申请STACK2大小的空间*/
if(p==NULL) /*如果p为空*/

printf("memory is overflow\n!!"); /*显示内存溢出*/
exit(0); /*退出*/

p->data=x; /*保存值x到新空间*/
p->link=top; /*新结点的后继为当前栈顶指针*/
top=p; /*新的栈顶指针为新插入的结点*/
return top; /*返回栈顶指针*/

STACK2 *pop2(STACK2 *top) /*出栈*/

STACK2 *q; /*定义临时变量*/
q=top; /*保存当前栈顶指针*/
top=top->link; /*栈顶指针后移*/
free(q); /*释放q*/
return top; /*返回栈顶指针*/

char topx2(STACK2 *top) /*读栈顶元素*/

if(top==NULL) /*栈是否为空*/

printf("Stack is null\n"); /*显示栈为空信息*/
return ''; /*返回空字符*/

return top->data; /*返回栈顶元素*/

STACK2 *ptop2(STACK2 *top,char *x) /*取栈顶元素,并删除栈顶元素*/

*x=topx2(top); /*读栈顶元素*/
top=pop2(top); /*删除栈顶元素*/
return top; /*返回栈顶指针*/

int empty2(STACK2 *top) /*判栈是否为空*/

if(top==NULL) /*如果为空*/
return 1; /*返回1*/
else
return 0; /*否则返回0*/


int text1(char *s)

setbkcolor(BLUE); /*设置背景颜色为蓝色*/
cleardevice(); /*清除屏幕*/
setcolor(12); /*设置文本颜色为淡红色*/
settextstyle(1, 0, 8);/*三重笔划字体, 放大8倍*/
outtextxy(120, 120, s); /*输出字符串s*/
setusercharsize(2, 1, 4, 1);/*水平放大2倍, 垂直放大4倍*/
setcolor(15); /*设置文本颜色为*白色/
settextstyle(3, 0, 5); /*无衬字笔划, 放大5倍*/
outtextxy(220, 220, s); /*输出字符串s*/
getch(); /*键盘输入任一字符*/
return ; /*返回*/
参考技术A 下面是我自己编的程序,跟你题目要求有点不一样,你可以自己修改下就OK了:
首先是头文件:
//head.h
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
//程序最大问题在于将推算过程与结果输出脱节,导致后面的输出函数编地不太好,而且输出的结果也不尽理想
//看来自己的编译原理没学好啊,好好改进,加油!!!

class game_24
int original[4];
float temp[4];
struct expression//此结构体专为了储存呆会要输出的表达式而建立,定义成一个结构体数组
float left_operate,right_operate;
char operater;
expression[3];
bool judge(int n,float result);//被speculate函数调用的函数
public:
bool input();//用于玩家输入
bool speculate(int n);//用于推算是否能够得出24
void output();//用于输出结果
;

bool game_24::input()//无法实现对float等的类型检查,有待完善
cout<<"请从1至13中输入4个自然数:"<<endl;
for(int i=0;i<4;i++)
cout<<"请输入第["<<i+1<<"]个数:"<<endl;
int check;
cin>>check;
if(check>0&&check<14) original[i]=check;
else
cout<<"输入有误,请重新输入"<<endl;
return false;


for(int i=0;i<4;i++)
temp[i]=(float)original[i];
return true;


bool game_24::judge(int n,float result)
if(n==2)
if(((result-24)<0.01)&&((result-24)>-0.01)) return true;//里面的判断语句不能用if((int)result==24),如果result=24.9,那么(int)result==24
else return false;

if(n==3) temp[1]=result;
if(n==4) temp[2]=result;
if(speculate(n-1)) return true;
return false;


bool game_24::speculate(int n)
float result,temp1,temp2,left_operate,right_operate;
int k;//K是为了用来移位的标志,是为了找出数组中除了已被使用的左右操作符的数
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i!=j)
k=0;//现场保护
left_operate=temp[i];
right_operate=temp[j];
if(n==4)//进行现场保护的同时进行移位,即变动数在数组中的位置,将在这次函数调用过程中还没用的值往前移动
while(k==i||k==j)k++;
temp1=temp[k];
temp2=temp[6-i-j-k];
temp[0]=temp1;
temp[1]=temp2;

if(n==3)
while(k==i||k==j)k++;
temp1=temp[k];
temp[0]=temp1;

expression[n-2].left_operate=left_operate;
expression[n-2].right_operate=right_operate;
result=left_operate+right_operate;
expression[n-2].operater='+';
if(judge(n,result)) return true;
result=left_operate-right_operate;
expression[n-2].operater='-';
if(judge(n,result)) return true;
result=left_operate*right_operate;
expression[n-2].operater='*';
if(judge(n,result)) return true;
if(right_operate!=0)//只有除数不为零时才能使用除法
result=left_operate/right_operate;
expression[n-2].operater='/';
if(judge(n,result)) return true;

if(n==3)//现场恢复
temp[i]=left_operate;
temp[j]=right_operate;
temp[k]=temp1;

if(n==4)
temp[i]=left_operate;
temp[j]=right_operate;
temp[k]=temp1;
temp[6-i-j-k]=temp2;


return false;


void game_24::output()
cout<<"推算成功:";
float result[2];
for(int i=1;i<3;i++)
if(expression[i].operater=='+') result[i-1]=expression[i].left_operate+expression[i].right_operate;
if(expression[i].operater=='-') result[i-1]=expression[i].left_operate-expression[i].right_operate;
if(expression[i].operater=='*') result[i-1]=expression[i].left_operate*expression[i].right_operate;
if(expression[i].operater=='/') result[i-1]=expression[i].left_operate/expression[i].right_operate;

if(result[1]==expression[1].left_operate||result[1]==expression[1].right_operate)
if(result[1]==expression[1].left_operate)
if(result[0]==expression[0].left_operate)//((x1@y1)@y2)@y3
cout<<"(("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<")"<<expression[1].operater<<expression[1].right_operate;
cout<<")"<<expression[0].operater<<expression[0].right_operate<<"=24"<<endl;
return;

if(result[0]==expression[0].right_operate)//x3@((x1@y1)@y2)
cout<<expression[0].left_operate<<expression[0].operater;
cout<<"(("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<")"<<expression[1].operater<<expression[1].right_operate<<")"<<"=24"<<endl;
return;


if(result[1]==expression[1].right_operate)
if(result[0]==expression[0].left_operate)//(x2@(x1@y1))@y3
cout<<"("<<expression[1].left_operate<<expression[1].operater;
cout<<"("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<"))"<<expression[0].operater<<expression[0].right_operate<<"=24"<<endl;
return;

if(result[0]==expression[0].right_operate)//y3@(x2@(x1@y1))
cout<<expression[0].left_operate<<expression[0].operater;
cout<<"("<<expression[1].left_operate<<expression[1].operater;
cout<<"("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate<<"))"<<"=24"<<endl;
return;



if(result[1]==expression[0].left_operate||result[1]==expression[0].right_operate)
if(result[1]==expression[0].left_operate)//(x1@y1)@(x2@y2)
cout<<"("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<")"<<expression[0].operater<<"(";
cout<<expression[1].left_operate<<expression[1].operater<<expression[1].right_operate<<")"<<"=24"<<endl;
return;

if(result[1]==expression[0].right_operate)//(x2@y2)@(x1@y1)
cout<<"("<<expression[1].left_operate<<expression[1].operater<<expression[1].right_operate;
cout<<")"<<expression[0].operater<<"(";
cout<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate<<")"<<"=24"<<endl;
return;



主函数:
#include <cstdlib>
#include <iostream>
#include "head.h"

using namespace std;

int main(int argc, char *argv[])

game_24 game;
bool test=game.input();
while(test==false)
test=game.input();

if(game.speculate(4)) game.output();
else cout<<"此输入无法求得24"<<endl;
system("PAUSE");
return EXIT_SUCCESS;

在Dev-C++中通过编译本回答被提问者采纳
参考技术B /*只做了一个普通算法实现的,没有特殊界面
我这里还有一个自己编辑的24点游戏的程序,可以向我讨要。不过不是针对你这个问题,而是普通的24点游戏的功能*/
Ver1.0
#include<iostream.h>

int Flag;

void Remember(float *a,int *ct,int *sq)//对给定的运算方式,给出计算式

int i,j,n,left,right,temp,s[3],tleft[2],tright[2],tsure[2];
char ch[16],c[6]='+','-','*','/','(',')';

for(i=0;i<4;i++)

ch[2*i]=i+48;

for(i=0;i<3;i++)

ch[2*i+1]=c[ct[i]];
s[i]=2*sq[i]+1;

n=6;
for(i=0;i<2;i++)//每个符号对应参与运算的内容

tsure[i]=1;
tleft[i]=s[i]-1;
tright[i]=s[i]+2;
for(j=0;j<i;j++)

if((tleft[i]<tright[j])&&(tright[i]>tright[j]))
tleft[i]=tleft[j];
if((tleft[i]<tleft[j])&&(tright[i]>tleft[j]))
tright[i]=tright[j];


for(i=1;i>=0;i--)//标记可以去掉的括号

for(j=2;j>=0;j--)
if(s[i+1]==tright[j])

if(ct[sq[j]]<2)

if(ct[sq[i+1]]<2)
tsure[j]=0;

else
tsure[j]=0;
break;

for(j=1;j>=0;j--)
if(s[i+1]+1==tleft[j])

if(ct[sq[j]]<2)

if(ct[sq[i+1]]==0)
tsure[j]=0;

else
if(ct[sq[i+1]]<2)
tsure[j]=0;
else
if(ct[sq[i+1]]==2)
tsure[j]=0;
break;


for(i=1;i>=0;i--)//去掉可以去掉的括号,并把应该加的括号加入

if(tsure[i]==1)

if(tright[i]==n)

n++;
ch[n]=c[5];

else

n++;
for(j=n;j>tright[i];j--)
ch[j]=ch[j-1];
ch[j]=c[5];
right=tright[i];
for(j=0;j<2;j++)

if(tright[j]>=right)
tright[j]++;
if(tleft[j]>right)
tleft[j]++;


n++;
for(j=n;j>tleft[i];j--)
ch[j]=ch[j-1];
ch[j]=c[4];
left=tleft[i];
for(j=0;j<2;j++)

if(tright[j]>left)
tright[j]++;
if(tleft[j]>=left)
tleft[j]++;



n++;
ch[n]='\0';
for(i=0;i<n;i++)//按照形式输出结果

if((ch[i]<'4')&&(ch[i]>='0'))

temp=(int)a[ch[i]-48];
if(temp==1)
cout<<'A';
if(temp==11)
cout<<'J';
if(temp==12)
cout<<'Q';
if(temp==13)
cout<<'K';
if((temp>1)&&(temp<11))
cout<<temp;

else
cout<<ch[i];

cout<<endl;


void CheckResult(float *a,int *ct,int *sq)//利用符号式运算方法检查结果是否24

float data[4];
int i,j,c[4],s[4];

for(i=0;i<4;i++)
data[i]=a[i];
for(i=0;i<3;i++)

c[i]=ct[i];
s[i]=sq[i];

for(i=0;i<3;i++)//按照次序运算此运算式

switch(c[s[i]])

case 0:
data[s[i]]+=data[s[i]+1];
for(j=s[i]+1;j<3-i;j++)
data[j]=data[j+1];
for(j=s[i];j<2-i;j++)
c[j]=c[j+1];
for(j=i;j<3;j++)
if(s[j]>s[i])
s[j]--;
break;
case 1:
data[s[i]]-=data[s[i]+1];
for(j=s[i]+1;j<3-i;j++)
data[j]=data[j+1];
for(j=s[i];j<2-i;j++)
c[j]=c[j+1];
for(j=i;j<3;j++)
if(s[j]>s[i])
s[j]--;
break;
case 2:
data[s[i]]*=data[s[i]+1];
for(j=s[i]+1;j<3-i;j++)
data[j]=data[j+1];
for(j=s[i];j<2-i;j++)
c[j]=c[j+1];
for(j=i;j<3;j++)
if(s[j]>s[i])
s[j]--;
break;
default :
if(data[s[i]+1]==0)

for(j=0;j<4;j++)
data[j]=1;

else

data[s[i]]/=data[s[i]+1];
for(j=s[i]+1;j<3-i;j++)
data[j]=data[j+1];
for(j=s[i];j<2-i;j++)
c[j]=c[j+1];
for(j=i;j<3;j++)
if(s[j]>s[i])
s[j]--;

break;


if(data[0]==24.0)//如果结果等于24,就输出结果并返回所有循环和递归

Remember(a,ct,sq);//把结果像式子一样输出
Flag=0;



void CaculateSequence(float *a,int *ct,int *sq,int n)//运算顺序全排列

int i,temp,sqc[3];

if(Flag==1)

for(i=0;i<3;i++)
sqc[i]=sq[i];
if(n>2)

CaculateSequence(a,ct,sqc,n-1);//递归方法求的所有情况
for(i=3-n+1;i<3;i++)

if(ct[sq[i]]!=ct[sq[3-n]])

temp=sqc[3-n];
sqc[3-n]=sqc[i];
sqc[i]=temp;
CaculateSequence(a,ct,sqc,n-1);//递归方法求的所有情况



else

CheckResult(a,ct,sqc);//检查结果
if(ct[sq[1]]!=ct[sq[2]])

temp=sqc[1];
sqc[1]=sqc[2];
sqc[2]=temp;
CheckResult(a,ct,sqc);//检查结果





void AllOperateQueue(float *a)//在每两个数间加入符号进行运算

int i,j,k,count[3],squence[3];

if(Flag==1)

for(i=0;i<3;i++)
squence[i]=i;
for(i=0;i<4;i++)//加入的符号的所有情况

count[0]=i;
for(j=0;j<4;j++)

count[1]=j;
for(k=0;k<4;k++)

count[2]=k;
CaculateSequence(a,count,squence,3);//对符号式运算






void AllDataQueue(float *a,int n)//把原数据进行全排列

int i;
float temp,data[4];

if(Flag==1)

for(i=0;i<4;i++)
data[i]=a[i];
if(n>2)

AllDataQueue(data,n-1);//递归实现
for(i=4-n+1;i<4;i++)

if(data[4-n]!=data[i])

temp=data[4-n];
data[4-n]=data[i];
data[i]=temp;
AllDataQueue(data,n-1);//递归



else

AllOperateQueue(data);//对排列好的数组进行如下运算
if(data[2]!=data[3])

temp=data[2];
data[2]=data[3];
data[3]=temp;
AllOperateQueue(data);//对排列好的数组进行如下运算





void main()

float data[4];
int i,j,cs[13];
char ch,c[4],st[14]="@123456789IPJ";

cout<<"Welcome to 24 scores game!"<<endl;
cout<<"Do you get ready to play(Y/N)?"<<endl;
cin>>ch;//判断是否进行游戏
for(i=0;i<13;i++)
cs[i]=i+1;
while((ch=='y')||(ch=='Y'))

Flag=1;//是否已经找到解
cout<<"Please input the fore number:"<<endl;
for(i=0;i<4;i++)

cin>>c;
if((c[0]=='1')&&(c[1]=='0'))//如果是10
c[0]='9';
else
c[0]=c[0]-1;//每一位比现在的数大1
for(j=0;j<13;j++)
if(c[0]==st[j])//如果输入是1-13的数
break;
if(j<13)
data[i]=(float)cs[j];
else
data[i]=(float)-0.0001;

AllDataQueue(data,4);//求解
if(Flag==1)
cout<<"There are no answer to this question!"<<endl;
cout<<"Do you want to play again(Y/N)?"<<endl;
cin>>ch;//判断是否继续

参考技术C 下面是我自己编的程序,跟你题目要求有点不一样,你可以自己修改下就OK了:
首先是头文件:
//head.h
#include
<vector>
#include
<iostream>
#include
<cstring>
#include
<cstdio>
using
namespace
std;
//程序最大问题在于将推算过程与结果输出脱节,导致后面的输出函数编地不太好,而且输出的结果也不尽理想
//看来自己的编译原理没学好啊,好好改进,加油!!!
class
game_24
int
original[4];
float
temp[4];
struct
expression//此结构体专为了储存呆会要输出的表达式而建立,定义成一个结构体数组
float
left_operate,right_operate;
char
operater;
expression[3];
bool
judge(int
n,float
result);//被speculate函数调用的函数
public:
bool
input();//用于玩家输入
bool
speculate(int
n);//用于推算是否能够得出24
void
output();//用于输出结果
;
bool
game_24::input()//无法实现对float等的类型检查,有待完善
cout<<"请从1至13中输入4个自然数:"<<endl;
for(int
i=0;i<4;i++)
cout<<"请输入第["<<i+1<<"]个数:"<<endl;
int
check;
cin>>check;
if(check>0&&check<14)
original[i]=check;
else

cout<<"输入有误,请重新输入"<<endl;
return
false;


for(int
i=0;i<4;i++)
temp[i]=(float)original[i];
return
true;

bool
game_24::judge(int
n,float
result)
if(n==2)
if(((result-24)<0.01)&&((result-24)>-0.01))
return
true;//里面的判断语句不能用if((int)result==24),如果result=24.9,那么(int)result==24
else
return
false;

if(n==3)
temp[1]=result;
if(n==4)
temp[2]=result;
if(speculate(n-1))
return
true;
return
false;

bool
game_24::speculate(int
n)
float
result,temp1,temp2,left_operate,right_operate;
int
k;//K是为了用来移位的标志,是为了找出数组中除了已被使用的左右操作符的数
for(int
i=0;i<n;i++)
for(int
j=0;j<n;j++)
if(i!=j)
k=0;//现场保护
left_operate=temp[i];
right_operate=temp[j];
if(n==4)//进行现场保护的同时进行移位,即变动数在数组中的位置,将在这次函数调用过程中还没用的值往前移动
while(k==i||k==j)k++;
temp1=temp[k];
temp2=temp[6-i-j-k];
temp[0]=temp1;
temp[1]=temp2;

if(n==3)
while(k==i||k==j)k++;
temp1=temp[k];
temp[0]=temp1;

expression[n-2].left_operate=left_operate;
expression[n-2].right_operate=right_operate;
result=left_operate+right_operate;
expression[n-2].operater='+';
if(judge(n,result))
return
true;
result=left_operate-right_operate;
expression[n-2].operater='-';
if(judge(n,result))
return
true;
result=left_operate*right_operate;
expression[n-2].operater='*';
if(judge(n,result))
return
true;
if(right_operate!=0)//只有除数不为零时才能使用除法
result=left_operate/right_operate;
expression[n-2].operater='/';
if(judge(n,result))
return
true;

if(n==3)//现场恢复
temp[i]=left_operate;
temp[j]=right_operate;
temp[k]=temp1;

if(n==4)
temp[i]=left_operate;
temp[j]=right_operate;
temp[k]=temp1;
temp[6-i-j-k]=temp2;


return
false;

void
game_24::output()
cout<<"推算成功:";
float
result[2];
for(int
i=1;i<3;i++)
if(expression[i].operater=='+')
result[i-1]=expression[i].left_operate+expression[i].right_operate;
if(expression[i].operater=='-')
result[i-1]=expression[i].left_operate-expression[i].right_operate;
if(expression[i].operater=='*')
result[i-1]=expression[i].left_operate*expression[i].right_operate;
if(expression[i].operater=='/')
result[i-1]=expression[i].left_operate/expression[i].right_operate;

if(result[1]==expression[1].left_operate||result[1]==expression[1].right_operate)
if(result[1]==expression[1].left_operate)
if(result[0]==expression[0].left_operate)//((x1@y1)@y2)@y3
cout<<"(("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<")"<<expression[1].operater<<expression[1].right_operate;
cout<<")"<<expression[0].operater<<expression[0].right_operate<<"=24"<<endl;
return;

if(result[0]==expression[0].right_operate)//x3@((x1@y1)@y2)
cout<<expression[0].left_operate<<expression[0].operater;
cout<<"(("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<")"<<expression[1].operater<<expression[1].right_operate<<")"<<"=24"<<endl;
return;


if(result[1]==expression[1].right_operate)
if(result[0]==expression[0].left_operate)//(x2@(x1@y1))@y3
cout<<"("<<expression[1].left_operate<<expression[1].operater;
cout<<"("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<"))"<<expression[0].operater<<expression[0].right_operate<<"=24"<<endl;
return;

if(result[0]==expression[0].right_operate)//y3@(x2@(x1@y1))
cout<<expression[0].left_operate<<expression[0].operater;
cout<<"("<<expression[1].left_operate<<expression[1].operater;
cout<<"("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate<<"))"<<"=24"<<endl;
return;



if(result[1]==expression[0].left_operate||result[1]==expression[0].right_operate)
if(result[1]==expression[0].left_operate)//(x1@y1)@(x2@y2)
cout<<"("<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate;
cout<<")"<<expression[0].operater<<"(";
cout<<expression[1].left_operate<<expression[1].operater<<expression[1].right_operate<<")"<<"=24"<<endl;
return;

if(result[1]==expression[0].right_operate)//(x2@y2)@(x1@y1)
cout<<"("<<expression[1].left_operate<<expression[1].operater<<expression[1].right_operate;
cout<<")"<<expression[0].operater<<"(";
cout<<expression[2].left_operate<<expression[2].operater<<expression[2].right_operate<<")"<<"=24"<<endl;
return;



主函数:
#include
<cstdlib>
#include
<iostream>
#include
"head.h"
using
namespace
std;
int
main(int
argc,
char
*argv[])

game_24
game;
bool
test=game.input();
while(test==false)
test=game.input();

if(game.speculate(4))
game.output();
else
cout<<"此输入无法求得24"<<endl;
system("PAUSE");
return
EXIT_SUCCESS;

在Dev-C++中通过编译
参考技术D #include<iostream>
#include<stdio.h>
#include<string>
#include<windows.h>
using namespace std;
int loc,i1,i2,i3,w1,w2,w3,k;
string x2,x3;
void alter(int x)

loc=x2.find(',');
if(loc<=-1)

cout<<"sorry,something wrong happened,and we have to terminate";
exit(1);
switch(x)
case 1:x2.replace(loc,1,"+");break;
case 2:x2.replace(loc,1,"-");break;
case 3:x2.replace(loc,1,"*");break;
case 4:x2.replace(loc,1,"/");break;

void main()
char ch[20],cg;
int i,s,a[4],ci=0,ww,j1,j2;
string x,xx;
while(1)
cout<<"请输入需要的24点数,数与数之间用逗号隔开,如8,7,6,2Enter"<<endl;
ci=0;
cin>>x;
s=x.find('-');
if(s>=0&&s<x.size())
cout<<"you choose to end this game\n";
break; /*查找是否有-号*/
for(i=0;i<x.size();i++)
cg=x.at(i);
switch(cg)
case 'A':x.replace(i,1,"1");break;
case 'J':x.replace(i,1,"11");break;
case 'Q':x.replace(i,1,"12");break;
case 'K':x.replace(i,1,"13");break; /*把A,K等字母替换成数字*/
default :;


x3=x2=x;
for(i=0;i<3;i++)
s=x.find(',');
xx.assign(x,0,s);
x.erase(0,s+1);
strcpy(ch,xx.data());
a[i]=atoi(ch);
strcpy(ch,x.data());
a[i]=atoi(ch);
int s1,s2;
for(i1=1;i1<=4;i1++) /*判断((A#B)#C)#D)是否能产生24,以后的循环与此同理*/
s1=s2=0;
ww=0;
switch(i1)
case 1:s1=a[0]+a[1];w1=1;break;
case 2:s1=a[0]-a[1];w1=2;break;
case 3:s1=a[0]*a[1];w1=3;break;
case 4:if((a[1]!=0)&&(!(a[0]%a[1])))

s1=a[0]/a[1];
else
ww=1;w1=4;break;

if(ww)
continue;
for(i2=1;i2<=4;i2++)
s2=0;
ww=0;
switch(i2)
case 1:s2=s1+a[2];w2=1;break;
case 2:s2=s1-a[2];w2=2;break;
case 3:s2=s1*a[2];w2=3;break;
case 4:if((a[2]!=0)&&(!(s1%a[2])))

s2=s1/a[2];
else
ww=1;w2=4;break;
if(ww)
continue;
j2=s2;
for(i3=1;i3<=4;i3++)
s2=j2;
switch(i3)
case 1:s2+=a[3];w3=1;break;
case 2:s2-=a[3];w3=2;break;
case 3:s2*=a[3];w3=3;break;
case 4:if((a[3]!=0)&&(!(s2%a[3])))

s2/=a[3];
else
ww=1;w3=4;break;
if(ww)
continue;

if(s2==24)

ci++;

x2.insert(ww,1,'(');
alter(w1);
loc=x2.find(',');

x2.insert(loc,1,')');
alter(w2);

x2.insert(ww,1,'(');
loc=x2.find(',');
x2.insert(loc,1,')');
alter(w3);
cout<<x2;
x2=x3;
system("pause");

s1=s2=j1=j2=w1=w2=w3=ww=0;
for(i1=1;i1<=4;i1++)
s1=s2=0;
ww=0;
switch(i1)
case 1:s1=a[1]+a[2];w1=1;break;
case 2:s1=a[1]-a[2];w1=2;break;
case 3:s1=a[1]*a[2];w1=3;break;
case 4:if((a[2]!=0)&&(!(a[1]%a[2])))

s1=a[1]/a[2];
else
ww=1;w1=4;break;

if(ww)
continue;

j1=s2;
for(i2=1;i2<=4;i2++)
s2=j1;
ww=0;
switch(i2)
case 1:s2=s1+a[0];w2=1;break;
case 2:s2=a[0]-s1;w2=2;break;
case 3:s2=s1*a[0];w2=3;break;
case 4:if((s1!=0)&&(!(a[0]%s1)))

s2=a[0]/s1;
else
ww=1;w2=4;break;
if(ww)
continue;
j2=s2;
for(i3=1;i3<=4;i3++)
s2=j2;
switch(i3)
case 1:s2+=a[3];w3=1;break;
case 2:s2-=a[3];w3=2;break;
case 3:s2*=a[3];w3=3;break;
case 4:if((a[3]!=0)&&(!(s2%a[3])))

s2/=a[3];
else
ww=1;w3=4;break;
if(ww)
continue;

if(s2==24)

ci++;
loc=x2.find(',');
loc++;
x2.insert(loc,1,'(');
alter(w2);
alter(w1);
loc=x2.find(',');
x2.insert(loc,1,')');
x2.insert(ww,1,'(');
loc=x2.find(',');
x2.insert(loc,1,')');
alter(w3);cout<<x2;
x2=x3;
system("pause");



s1=s2=j1=j2=w1=w2=w3=ww=0;
for(i1=1;i1<=4;i1++)
s1=s2=0;
ww=0;
switch(i1)
case 1:s1=a[1]+a[2];w1=1;break;
case 2:s1=a[1]-a[2];w1=2;break;
case 3:s1=a[1]*a[2];w1=3;break;
case 4:if((a[2]!=0)&&(!(a[1]%a[2])))

s1=a[1]/a[2];
else
ww=1;w1=4;break;

if(ww)
continue;

j1=s2;
for(i2=1;i2<=4;i2++)
s2=j1;
ww=0;
switch(i2)
case 1:s2=s1+a[3];w2=1;break;
case 2:s2=s1-a[3];w2=2;break;
case 3:s2=s1*a[3];w2=3;break;
case 4:if((a[3]!=0)&&(!(s1%a[3])))

s2=s1/a[3];
else
ww=1;w2=4;break;
if(ww)
continue;
j2=s2;
for(i3=1;i3<=4;i3++)
s2=j2;
switch(i3)
case 1:s2+=a[0];w3=1;break;
case 2:s2=a[0]-s2;w3=2;break;
case 3:s2*=a[0];w3=3;break;
case 4:if((s2!=0)&&(!(a[0]%s2)))

s2=a[0]/s2;
else
ww=1;w3=4;break;
if(ww)
continue;

if(s2==24)


ci++;
loc=x2.find(',');
loc++;
x2.insert(loc,1,'(');
x2.append(1,')');
alter(w3);

loc=x2.find('(');
x2.insert(loc,1,'(');

alter(w1);

loc=x2.find(',');
x2.insert(loc,1,')');

alter(w2);

cout<<x2;
x2=x3;
system("pause");




/*zfor*/

/*2dafor*/
cout<<"***********";
s1=s2=j1=j2=w1=w2=w3=ww=0;
for(i1=1;i1<=4;i1++)
s1=s2=0;
ww=0;

switch(i1)
case 1:s1=a[3]+a[2];w1=1;break;
case 2:s1=a[2]-a[3];w1=2;break;
case 3:s1=a[3]*a[2];w1=3;break;
case 4:if((a[3]!=0)&&(!(a[2]%a[3])))

s1=a[2]/a[3];
else
ww=1;w1=4;break;

if(ww)
continue;

j1=s2;
for(i2=1;i2<=4;i2++)
s2=j1;
ww=0;
switch(i2)
case 1:s2=a[0]+a[1];w2=1;break;
case 2:s2=a[0]-a[1];w2=2;break;
case 3:s2=a[0]*a[1];w2=3;break;
case 4:if((a[1]!=0)&&(!(a[0]%a[1])))

s2=a[0]/a[1];
else
ww=1;w2=4;break;
if(ww)
continue;
j2=s2;
for(i3=1;i3<=4;i3++)
s2=j2;
switch(i3)
case 1:s2+=s1;w3=1;break;
case 2:s2-=s1;w3=2;break;
case 3:s2*=s1;w3=3;break;
case 4:if((s1!=0)&&(!(s2%s1)))

s2/=s1;
else
ww=1;w3=4;break;
if(ww)
continue;

if(s2==24)
x2.insert(ww,1,'(');
x2.append(1,')');
alter(w2);
loc=x2.find(',');
x2.insert(loc,1,')');
loc+=2;
x2.insert(loc,1,'(');

alter(w3);
alter(w1);
cout<<x2;
x2=x3;
system("pause");




/*zfor*/

/*1dafor*/
s1=s2=j1=j2=w1=w2=w3=ww=0;
for(i1=1;i1<=4;i1++)
s1=s2=0;
ww=0;

switch(i1)
case 1:s1=a[3]+a[2];w1=1;break;
case 2:s1=a[2]-a[3];w1=2;break;
case 3:s1=a[3]*a[2];w1=3;break;
case 4:if((a[3]!=0)&&(!(a[2]%a[3])))

s1=a[2]/a[3];
else
ww=1;w1=4;break;

if(ww)
continue;

j1=s2;
for(i2=1;i2<=4;i2++)
s2=j1;
ww=0;
switch(i2)
case 1:s2=s1+a[1];w2=1;break;
case 2:s2=a[1]-s1;w2=2;break;
case 3:s2=s1*a[1];w2=3;break;
case 4:if((s1!=0)&&(!(a[1]%s1)))

s2=a[1]/s1;
else
ww=1;w2=4;break;
if(ww)
continue;
j2=s2;
for(i3=1;i3<=4;i3++)
s2=j2;
switch(i3)
case 1:s2+=a[0];w3=1;break;
case 2:s2=a[0]-s2;w3=2;break;
case 3:s2*=a[0];w3=3;break;
case 4:if((s2!=0)&&(!(a[0]%s2)))

s2=a[0]/s2;
else
ww=1;w3=4;break;
if(ww)
continue;

if(s2==24)
loc=x2.find(',');
loc++;
x2.insert(loc,1,'(');
x2.append(1,')');
alter(w3);

loc=x2.find(',');
loc++;
x2.insert(loc,1,'(');

alter(w2);

x2.append(1,')');
alter(w1);

cout<<x2;
x2=x3;
system("pause");




/*zfor*/

/*1dafor*/

if(!ci)
cout<<"不能实现24点运算\n\n";
Sleep(3000);
system("cls");

/*whie*/



我现在刚上大一,所以编写的代码看起来很幼稚,请高手莫笑。。。。。

[LeetCode] 24 Game 二十四点游戏

 

You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through */+-()to get the value of 24.

Example 1:

Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24

 

Example 2:

Input: [1, 2, 1, 2]
Output: False

 

Note:

  1. The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  2. Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
  3. You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

 

s

 

 

 

 

以上是关于利用c++ 做二十四点游戏。请高手们帮忙!谢谢的主要内容,如果未能解决你的问题,请参考以下文章

winform窗体问题`急等`请高手们帮忙解决

二十四点游戏

各位高手们,帮忙告诉我一下,我想建个团队,但那个百度图片网址怎么填?

Scala开发二十四点游戏

请高手解答在windows7中的为此网络启用联邦信息处理标准(FIPS)兼容究竟是啥意思大神们帮帮忙

[LeetCode] 24 Game 二十四点游戏