算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; }