帮忙给个方法或者思路

文~若 2010-07-27 05:38:13
计算24是流行的扑克游戏。其方法是任意取出4 张牌,A J Q K 王牌 算 1,其它
牌按点数计算,花色不计。目标是通过加、减、乘、除和括号最终算出24。设计一个程序
输入4个数字(1~10),则列出所有可能计算结果为24的方案。要求:
方案不能重复(加法乘法交换律等算不同方案)。
计算中局部可以为分数,结果为整数即可(如 3 3 7 7 算法: (3 + 3/7)*7)
如果没有找到方案输出:无解。
...全文
101 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcyjy2009 2010-08-12
  • 打赏
  • 举报
回复
else if(k==2)
{
temp[0]=digit[0]+'0';
temp[1]=converse(op[0]);
temp[2]='(';
temp[3]=digit[1]+'0';
temp[4]=converse(op[1]);
temp[5]=digit[2]+'0';
temp[6]=')';
temp[7]=converse(op[2]);
temp[8]=digit[3]+'0';
temp[9]='\0';


func(temp);
}
else if(k==3)
{
temp[0]=digit[0]+'0';
temp[1]=converse(op[0]);
temp[2]=digit[1]+'0';
temp[3]=converse(op[1]);
temp[4]='(';
temp[5]=digit[2]+'0';
temp[6]=converse(op[2]);
temp[7]=digit[3]+'0';
temp[8]=')';
temp[9]='\0';
func(temp);

}
else if(k==4)
{
temp[0]='(';
temp[1]=digit[0]+'0';
temp[2]=converse(op[0]);
temp[3]=digit[1]+'0';
temp[4]=converse(op[1]);
temp[5]=digit[2]+'0';
temp[6]=')';
temp[7]=converse(op[2]);
temp[8]=digit[3]+'0';
temp[9]='\0';
func(temp);
}
else if(k==5)
{
temp[0]=digit[0]+'0';
temp[1]=converse(op[0]);
temp[2]='(';
temp[3]=digit[1]+'0';
temp[4]=converse(op[1]);
temp[5]=digit[2]+'0';
temp[6]=converse(op[2]);
temp[7]=digit[3]+'0';
temp[8]=')';
temp[9]='\0';
func(temp);
}
else if(k==6)
{
temp[0]='(';
temp[1]='(';
temp[2]=digit[0]+'0';
temp[3]=converse(op[0]);
temp[4]=digit[1]+'0';
temp[5]=')';
temp[6]=converse(op[1]);
temp[7]=digit[2]+'0';
temp[8]=')';
temp[9]=converse(op[2]);
temp[10]=digit[3]+'0';
temp[11]='\0';
func(temp);
}
else if(k==7)
{
temp[0]='(';
temp[1]=digit[0]+'0';
temp[2]=converse(op[0]);
temp[3]='(';
temp[4]=digit[1]+'0';
temp[5]=converse(op[1]);
temp[6]=digit[2]+'0';
temp[7]=')';
temp[8]=')';
temp[9]=converse(op[2]);
temp[10]=digit[3]+'0';
temp[11]='\0';
func(temp);
}
else if(k==8)
{
temp[0]=digit[0]+'0';
temp[1]=converse(op[0]);
temp[2]='(';
temp[3]='(';
temp[4]=digit[1]+'0';
temp[5]=converse(op[1]);
temp[6]=digit[2]+'0';
temp[7]=')';
temp[8]=converse(op[2]);
temp[9]=digit[3]+'0';
temp[10]=')';
temp[11]='\0';
func(temp);

}
else if(k==9)
{
temp[0]=digit[0]+'0';
temp[1]=converse(op[0]);
temp[2]='(';
temp[3]=digit[1]+'0';
temp[4]=converse(op[1]);
temp[5]='(';
temp[6]=digit[2]+'0';
temp[7]=converse(op[2]);
temp[8]=digit[3]+'0';
temp[9]=')';
temp[10]=')';
temp[11]='\0';
func(temp);
}
else
{
}


}


system("pause");
return 0;
}
hcyjy2009 2010-08-12
  • 打赏
  • 举报
回复
void func(char Mstr[80])
{
char str[80];
int len;


char op;
int num1;
int num2;
int result;

int i,j,k;
char c;
//printf("\n 请输入要求值的表达式 \n");
//gets(str);
strcpy(str,Mstr);


DisposeStr(str);

len=strlen(str);



//printf("%d",len);

InitOpndStack();
InitOptrStack();
PushOptrStack('#');

i=0;
c=str[i++];
while(c!='#' || GetTopOptr()!='#')
{
if(isOpnd(c)){PushOpndStack(c-'0');c=str[i++];}
else
switch(Precede(GetTopOptr(),c))
{
case '<':PushOptrStack(c);c=str[i++];break;
case '=':PopOptrStack();c=str[i++];break;
case '>':
op=PopOptrStack();
num2=PopOpndStack();
num1=PopOpndStack();
result=cal(num1,op,num2);
if(flag==0)
PushOpndStack(result);
break;
default:break;

}
if(flag==1)break;
}


if(flag==1)
{flag=0;return;}
else
{
int total=GetTopOpnd();
if(total==24)
printf("\n %s 结果是 %d\n",Mstr,total);
}

}
char converse(int i)
{
switch(i-1)
{
case 0:return '+';break;
case 1:return '-';break;
case 2:return '*';break;
case 3:return '/';break;
default:break;
}
}
int main()
{


char temp[80];
int i=0;
int digit[4];
int op[3];
int k;
for(digit[0]=1;digit[0]<=9;digit[0]++)
for(digit[1]=1;digit[1]<=9;digit[1]++)
for(digit[2]=1;digit[2]<=9;digit[2]++)
for(digit[3]=1;digit[3]<=9;digit[3]++)
for(op[0]=1;op[0]<=4;op[0]++)
for(op[1]=1;op[1]<=4;op[1]++)
for(op[2]=1;op[2]<=4;op[2]++)
{

for(k=0;k<10;k++)
if(k==0)
{
temp[0]=digit[0]+'0';
temp[1]=converse(op[0]);
temp[2]=digit[1]+'0';
temp[3]=converse(op[1]);
temp[4]=digit[2]+'0';
temp[5]=converse(op[2]);
temp[6]=digit[3]+'0';
temp[7]='\0';

func(temp);
}
else if(k==1)
{
temp[0]='(';
temp[1]=digit[0]+'0';
temp[2]=converse(op[0]);
temp[3]=digit[1]+'0';
temp[4]=')';
temp[5]=converse(op[1]);
temp[6]=digit[2]+'0';
temp[7]=converse(op[2]);
temp[8]=digit[3]+'0';
temp[9]='\0';
func(temp);
}
hcyjy2009 2010-08-12
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAXSIZE 100

int flag=0;
char ptr[7]={'+','-','*','/','(',')','#'};
char table[7][7]=
{
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
int LocateVex(char a)
{
for(int i=0;i<7;i++)
if(ptr[i]==a)
return i;
}

typedef struct OpndStack
{
int data[MAXSIZE];
int top;
}OpndStack;

typedef struct OptrStack
{
char data[MAXSIZE];
int top;
}OptrStack;

OpndStack opnd;
OptrStack optr;


void InitOpndStack()
{
opnd.top=-1;
}

void InitOptrStack()
{
optr.top=-1;
}

void DisposeStr(char str[])
{
int len;
len=strlen(str);
str[len]='#';
str[len+1]='\0';
}



void PushOpndStack(int a)
{
opnd.data[++opnd.top]=a;
}
void PushOptrStack(char c)
{
optr.data[++optr.top]=c;
}

int PopOpndStack()
{
return opnd.data[opnd.top--];
}
char PopOptrStack()
{
return optr.data[optr.top--];
}

char GetTopOptr()
{
return optr.data[optr.top];
}

int GetTopOpnd()
{
return opnd.data[opnd.top];
}
bool isOpnd(char c)
{
if(c>='0' && c<='9')
return true;
return false;
}

char Precede(char a,char c)
{
int i=LocateVex(a);
int j=LocateVex(c);
return table[i][j];
}

int cal(int a,char op,int b)
{
if(b==0 && op=='/'){flag=1;return -1;}
switch(op)
{
case '+':return a+b;break;
case '-':return a-b;break;
case '*':return a*b;break;
case '/':return a/b;break;
default:break;
}
}
bolink5 2010-07-27
  • 打赏
  • 举报
回复
huayiluo 2010-07-27
  • 打赏
  • 举报
回复
你的要求,和那一章讲得一模一样,我现在还记得一些。第一个算法是用递归实现的。
huayiluo 2010-07-27
  • 打赏
  • 举报
回复
你的意思是24点算法?我看过,在那个微软编程之美的有。希望能你有帮助。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧