I. Title Requirements
Rules of the game:
Remove four cards at A time from poker. Using addition, subtraction, multiplication and division, the first one to get 24 wins. (where J stands for 11, Q stands for 12, K stands for 13, and A stands for 1).
Basic requirements:
The program automatically lists all the possible expressions of 24, and solves the problem with a good language (C/C++/Java or other).
1. Good program style (using custom comment template)
2. The list expression is not duplicate.
II. Topic analysis
The general idea is to enumerate the expressions after the arrangement and combination of the four numbers, select the ones with the result of 24, and select the output that conforms to the mathematical rule, so that the computer can work out all the expressions related to the four numbers, and select the calculation output of 24. So determine the thinking and exhaust. It can be divided into several modules, the main function, the fun function to convert four operations, for loop, if selection, scanf, printf.
Three. Commissioning
When assigning the input value to num+i, the address character &, cannot be added in front of it, otherwise the valid result cannot be output
Four. Test
Input a set of data can be output normally, but there are repeated equations
V. compilation results
The illegal operation in the compilation result is the equation not equal to 24, and the other valid equations are the equation equal to 24.
Six. Conclusion
Through this study, I have learned a lot. This program is not very difficult, but the algorithm is cumbersome and needs to be careful. Through this program design, we can also apply the theoretical knowledge we usually learn to practical problems, which is helpful for our understanding and application. Moreover, the program design itself is more interesting, which can inspire us to conduct in-depth research. I hope that I can make full use of the advantages of the computer itself in the future study of professional courses, and make use of the programming to complete some problems that are difficult to complete with manual calculation.
The procedure is as follows:
#include<stdio.h> //Using switch statement to transform four operations double fun(double a1,double a2,int b) { switch(b) { case 0:return (a1+a2); case 1:return (a1-a2); case 2:return (a1*a2); case 3:return (a1/a2); } } void main() { int i,j,k,l,n,m,r,save[4]; double num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111; char sign[5]="+-*/"; //Loop input with for statement printf("Please enter 4 numbers,Space between numbers:"); for(i=0;i<4;i++) { scanf("%lf",num+i); save[i]=num[i]; } //Using for and if statements to arrange and combine numbers for(i=0;i<4;i++) for(j=0;j<4;j++) if(j!=i) { for(k=0;k<4;k++) if(k!=i&&k!=j) { for(l=0;l<4;l++) if(l!=i&&l!=j&&l!=k) { //Using two paragraphs to calculate in two cases for(n=0;n<4;n++) for(m=0;m<4;m++) for(r=0;r<4;r++) { tem1=fun(num[i],num[j],n); tem2=fun(tem1,num[k],m); tem3=fun(tem2,num[l],r); //for loop implements the permutation and combination of operations. The above is a case (without brackets) //The output statement is as follows if(tem3==24.0)printf("{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]); else if(tem3==-24.0)printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[ i],sign[n],save[j],sign[r],save[l]); else if(tem3==1.0/24.0)printf("%d%c{(%d%c%d)%c%d}=24\n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k]); else if(tem3==-1.0/24.0)printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j]); //Here's another else { tem1=fun(num[i],num[j],n); tem2=fun(num[k],num[l],r); if(tem2 == 2 && m == 3) printf("An illegal operation is omitted here\n"); //Here is the combination of two else { tem3=fun(tem1,tem2,m); if(tem3==24.0) printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m], save[k],sign[r],save[l]); } } } } } } }