当前位置: 代码迷 >> C语言 >> 一个三八三八的问题!
  详细解决方案

一个三八三八的问题!

热度:326   发布时间:2005-04-25 12:57:00.0
一个三八三八的问题!
有四个数3、3、8、8 加减乘除都可以用,但每数只许用一次,最后结果怎么样才能让它等于24呢?用程序怎么实现算法是什么?
----------------解决方案--------------------------------------------------------
自己顶一下吧1
----------------解决方案--------------------------------------------------------
不会,也帮你顶一下!
----------------解决方案--------------------------------------------------------
8/(3-(8/3))
----------------解决方案--------------------------------------------------------
以下是引用knocker在2005-4-26 9:32:10的发言: 8/(3-(8/3))
算法呢,把算法搞出来,如我输入任意的4个数都要让它加减乘除等于24,改怎么做?
----------------解决方案--------------------------------------------------------
我用C++做过一个24点的控制台程序,可能有点繁琐,有兴趣看看吧。。 http://bbs.bc-cn.net/bbs/dispbbs.asp?BoardID=5&ID=15892
----------------解决方案--------------------------------------------------------
我想只能是穷举法了!
----------------解决方案--------------------------------------------------------
8/(3-8/3)        不就搞定了嘛,至于算法,我还没搞出来
----------------解决方案--------------------------------------------------------

算24点的程序: #include <stdio.h> #include <stdlib.h>

int A,B,C,D; int a,b,c,d;

void Pailie(int i) { switch(i) { case 1:A=a,B=b,C=c,D=d;break; case 2:A=a,B=b,C=d,D=c;break; case 3:A=a,B=c,C=b,D=d;break; case 4:A=a,B=c,C=d,D=b;break; case 5:A=a,B=d,C=b,D=c;break; case 6:A=a,B=d,C=c,D=b;break;

case 7:A=b,B=a,C=c,D=d;break; case 8:A=b,B=a,C=d,D=c;break; case 9:A=b,B=c,C=a,D=d;break; case 10:A=b,B=c,C=d,D=a;break; case 11:A=b,B=d,C=c,D=a;break; case 12:A=b,B=d,C=a,D=c;break;

case 13:A=c,B=a,C=d,D=b;break; case 14:A=c,B=a,C=b,D=d;break; case 15:A=c,B=b,C=a,D=d;break; case 16:A=c,B=b,C=d,D=a;break; case 17:A=c,B=d,C=a,D=b;break; case 18:A=c,B=d,C=b,D=a;break;

case 19:A=d,B=a,C=b,D=c;break; case 20:A=d,B=a,C=c,D=b;break; case 21:A=d,B=b,C=a,D=c;break; case 22:A=d,B=b,C=c,D=a;break; case 23:A=d,B=c,C=a,D=b;break; case 24:A=d,B=c,C=b,D=a;break; } }

int Find() { int i,j,sum; float result;

sum=a+b+c+d; for(i=1;i<=37;i++) switch(i) { case 1: if(sum==24) { printf("The answer is %d+%d+%d+%d\n\n",a,b,c,d); return 0; } break; case 2: if(sum>24) { if(24==sum-2*a) { printf("The answer is %d+%d+%d-%d\n\n",b,c,d,a); return 0; } if(24==sum-2*b) { printf("The answer is %d+%d+%d-%d\n\n",a,c,d,b); return 0; } if(24==sum-2*c) { printf("The answer is %d+%d+%d-%d\n\n",a,b,d,c); return 0; } if(24==sum-2*d) { printf("The answer is %d+%d+%d-%d\n\n",a,b,c,d); return 0; } } break; case 3: for(j=1;j<=24;j++) { Pailie(j); result=A+B+C*D; if(result==24) { printf("The answer is %d+%d+%d*%d\n\n",A,B,C,D); return 0; } } break; case 4: for(j=1;j<=24;j++) { Pailie(j); result=A+(B+C)*D; if(result==24) { printf("The answer is %d+(%d+%d)*%d\n\n",A,B,C,D); return 0; } } break; case 5: for(j=1;j<=24;j++) { Pailie(j); result=(A+B+C)*D; if(result==24) { printf("The answer is (%d+%d+%d)*%d\n\n",A,B,C,D); return 0; } } break; case 6: for(j=1;j<=24;j++) { Pailie(j); result=(A+B)*(C+D); if(result==24) { printf("The answer is (%d+%d)*(%d+%d)\n\n",A,B,C,D); return 0; } } break; case 7: for(j=1;j<=24;j++) { Pailie(j); result=A+B*C*D; if(result==24) { printf("The answer is %d+%d*%d*%d\n\n",A,B,C,D); return 0; } } break; case 8: for(j=1;j<=24;j++) { Pailie(j); result=A*B+C*D; if(result==24) { printf("The answer is %d*%d+%d*%d\n\n",A,B,C,D); return 0; } } break; case 9: for(j=1;j<=24;j++) { Pailie(j); result=(A+B)*C*D; if(result==24) { printf("The answer is (%d+%d)*%d*%d\n\n",A,B,C,D); return 0; } } break; case 10: for(j=1;j<=24;j++) { Pailie(j); result=(A+B*C)*D; if(result==24) { printf("The answer is (%d+%d*%d)*%d\n\n",A,B,C,D); return 0; } } break; case 11: result=A*B*C*D; if(result==24) { printf("The answer is %d*%d*%d*%d\n\n",A,B,C,D); return 0; } break; case 12: for(j=1;j<=24;j++) { Pailie(j); result=(A-B)*(C-D); if(result==24) { printf("The answer is (%d-%d)*(%d-%d)\n\n",A,B,C,D); return 0; } } break; case 13: for(j=1;j<=24;j++) { Pailie(j); result=A*(B-C-D); if(result==24) { printf("The answer is %d*(%d-%d-%d)\n\n",A,B,C,D); return 0; } } break; case 14: for(j=1;j<=24;j++) { Pailie(j); result=A*B-C-D; if(result==24) { printf("The answer is %d*%d-%d-%d\n\n",A,B,C,D); return 0; } } break; case 15: for(j=1;j<=24;j++) { Pailie(j); result=A*(B-C)-D; if(result==24) { printf("The answer is %d*(%d-%d)-%d\n\n",A,B,C,D); return 0; } } break; case 16: for(j=1;j<=24;j++) { Pailie(j); result=A*B*C-D; if(result==24) { printf("The answer is %d*%d*%d-%d\n\n",A,B,C,D); return 0; } } break; case 17: for(j=1;j<=24;j++) { Pailie(j); result=A*B-C*D; if(result==24) { printf("The answer is %d*%d-%d*%d\n\n",A,B,C,D); return 0; } } break; case 18: for(j=1;j<=24;j++) { Pailie(j); result=(A-B)*C*D; if(result==24) { printf("The answer is (%d-%d)*%d*%d\n\n",A,B,C,D); return 0; } } break; case 19: for(j=1;j<=24;j++) { Pailie(j); result=(A-B*C)*D; if(result==24) { printf("The answer is (%d-%d*%d)*%d\n\n",A,B,C,D); return 0; } } break; case 20: for(j=1;j<=24;j++) { Pailie(j); result=A+B+1.0*C/D; if(result==24) { printf("The answer is %d+%d+%d/%d\n\n",A,B,C,D); return 0; } } break; case 21: for(j=1;j<=24;j++) { Pailie(j); result=A*D*1.0/(B*D-C); if(result==24) { printf("The answer is %d/(%d-%d/%d)\n\n",A,B,C,D); return 0; } } break; case 22: for(j=1;j<=24;j++) { Pailie(j); result=A*B*C*1.0/D; if(result==24) { printf("The answer is %d*%d*%d/%d\n\n",A,B,C,D); return 0; } } break; case 23: for(j=1;j<=24;j++) { Pailie(j); result=A*B*1.0/C/D; if(result==24) { printf("The answer is %d*%d/%d/%d\n\n",A,B,C,D); return 0; } }break; case 24: for(j=1;j<=24;j++) { Pailie(j); result=(A+B-C)*1.0/D; if(result==24) { printf("The answer is (%d+%d-%d)/%d\n\n",A,B,C,D); return 0; } } break; case 25: for(j=1;j<=24;j++) { Pailie(j); result=(A+B)*C-D; if(result==24) { printf("The answer is (%d+%d)*%d-%d\n\n",A,B,C,D); return 0; } } break; case 26: for(j=1;j<=24;j++) { Pailie(j); result=A*B+C-D; if(result==24) { printf("The answer is %d*%d+%d-%d\n\n",A,B,C,D); return 0; } } break; case 27: for(j=1;j<=24;j++) { Pailie(j); result=(A+B)*(C-D); if(result==24) { printf("The answer is (%d+%d)*(%d-%d)\n\n",A,B,C,D); return 0; } } break; case 28: for(j=1;j<=24;j++) { Pailie(j); result=(A-B)*C+D; if(result==24) { printf("The answer is (%d-%d)*%d+%d\n\n",A,B,C,D); return 0; } } break; case 29: for(j=1;j<=24;j++) { Pailie(j); result=(A+B)*1.0*C/D; if(result==24) { printf("The answer is (%d+%d)*%d/%d\n\n",A,B,C,D); return 0; } } break; case 30: for(j=1;j<=24;j++) { Pailie(j); result=A+B*1.0*C/D; if(result==24) { printf("The answer is %d+%d*%d/%d\n\n",A,B,C,D); return 0; } } break; case 31: for(j=1;j<=24;j++) { Pailie(j); result=A*B*1.0/(C+D); if(result==24) { printf("The answer is %d*%d/(%d+%d)\n\n",A,B,C,D); return 0; } } break; case 32: for(j=1;j<=24;j++) { Pailie(j); result=(A-B)*1.0*C/D; if(result==24) { printf("The answer is (%d-%d)*%d/%d\n\n",A,B,C,D); return 0; } } break; case 33: for(j=1;j<=24;j++) { Pailie(j); result=A*B*1.0/(C-D); if(result==24) { printf("The answer is %d*%d/(%d-%d)\n\n",A,B,C,D); return 0; } } break; case 34: for(j=1;j<=24;j++) { Pailie(j); result=A*(B*D-C)*1.0/D; if(result==24) { printf("The answer is %d*(%d-%d/%d)\n\n",A,B,C,D); return 0; } } break; case 35: for(j=1;j<=24;j++) { Pailie(j); result=A*(B*D+C)*1.0/D; if(result==24) { printf("The answer is %d*(%d+%d/%d)\n\n",A,B,C,D); return 0; } } break; case 36: for(j=1;j<=24;j++) { Pailie(j); result=A*(B/C-D); if(result==24) { printf("The answer is %d*(%d/%d-%d)\n\n",A,B,C,D); return 0; } } break; case 37: for(j=1;j<=24;j++) { Pailie(j); result=A*B-1.0*C/D; if(result==24) { printf("The answer is %d*%d-%d/%d\n\n",A,B,C,D); return 0; } } printf("NO ANSWER!\n\n"); return(0); break; } }

int main() { int i,flag=0; char choice;

puts("================================================================"); puts("\t\t\t欢迎参与24点游戏"); puts("================================================================"); puts("");

START: a=b=c=d=0; while(a<1||b<1||c<1||d<1||a>10||b>10||c>10||d>10) { printf("Please enter four num(1~10):"); scanf("%d%d%d%d",&a,&b,&c,&d); } printf("%5d%5d%5d%5d\n",a,b,c,d); Find();

fflush(stdin); printf("Test again or exit(Press 'Y' or other key):"); choice=getchar(); if(choice=='Y'||choice=='y') { printf("%c\n",choice); goto START; }

return 0; }


----------------解决方案--------------------------------------------------------
真是经典!谢谢了!
----------------解决方案--------------------------------------------------------
  相关解决方案