当前位置:   article > 正文

计算24点问题,有输出方案_24点计算如果能输出1

24点计算如果能输出1

24点游戏:

       24点是一种益智游戏,24点是把4个整数(一般是正整数)通过加减乘除以及括号运算,使最后的计算结果是24的一个数学游戏,24点可以考验人的智力和数学敏感性,它能在游戏中提高人们的心算能力。
       24点通常是使用扑克牌来进行游戏的,一副牌中抽去大小王后还剩下52张(如果初练也可只用1~10这40张牌),任意抽取4张牌(称为牌组),用加、减、乘、除(可加括号)把牌面上的数算成24。每张牌必须只能用一次,如抽出的牌是3、8、8、9,那么算式为(9-8)×8×3或3×8÷(9-8)或(9-8÷8)×3等。(摘自百度百科)


算法思路:

因为有4个数,所以可以用暴力搜索所有可能情况的方式。但注意4个数的组合方式有两种:

一种是用一个数和剩下的一个数进行运算,得到的结果再和下一个数运算,一个一个数的运算得到24;

另一种是用两个数运算得到一个结果,用另两个数得到第二个结果,两个结果之间再运算得到24.

我是用广搜做的,也练了一下输出方案,具体看代码。如果想看到所有的方案,把两个找到结果地方的break和return给注释掉,就可以了(会有重复的,懒得改了。。。)

代码:

  1. #include<stdio.h>
  2. #include<queue>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<cstring>
  6. using namespace std;
  7. #define eps 1e-6
  8. struct node{
  9. double ans;
  10. int State;
  11. int op[4];
  12. int ch[5];
  13. int p;
  14. };
  15. int a[4];
  16. void output(node x){
  17. char s[30]={};
  18. int l = 14,r = 15;
  19. s[r++] = x.ch[0]+'0';
  20. for (int i = 1; i < 4; i++)
  21. {
  22. switch(x.op[i])
  23. {
  24. case( 0 ):s[r++]='+';s[r++]=x.ch[i]+'0';break;
  25. case( 1 ):s[r++]='-';s[r++]=x.ch[i]+'0';break;
  26. case( 2 ):s[l--]='-';s[l--]=x.ch[i]+'0';break;
  27. case( 3 ):s[r++]='*';s[r++]=x.ch[i]+'0';break;
  28. case( 4 ):s[l--]='/';s[l--]=x.ch[i]+'0';break;
  29. case( 5 ):s[r++]='/';s[r++]=x.ch[i]+'0';break;
  30. default:break;
  31. }
  32. if(i<3){
  33. s[l--] = '(';
  34. s[r++] = ')';
  35. }
  36. }
  37. printf("一种可以的计算方案:\t");
  38. for (int i = l+1; i < r; i++)
  39. {
  40. if(s[i] > '0')printf("%d",s[i]-'0');
  41. else printf("%c",s[i]);
  42. }printf("\n");
  43. return ;
  44. }
  45. void print(char s[]){
  46. for (int i = 0; i < 5; i++)
  47. {
  48. if(s[i] > '0')printf("%d",s[i]-'0');
  49. else printf("%c",s[i]);
  50. }
  51. }
  52. bool check(int s,int d,int f,int g){
  53. int flag = 0;
  54. double h1,h2;
  55. double ans;
  56. char x[6]={},y[6]={};
  57. x[0] = y[0] = '(';
  58. x[4] = y[4] = ')';
  59. x[5] = y[5] = '\0';
  60. for (int j = 0; j < 6; j++)
  61. {
  62. if(j==0) {h1 = s + d; x[1] = s+'0';x[2] = '+';x[3] = d+'0';}
  63. else if(j==1) {h1 = s - d; x[1] = s+'0';x[2] = '-';x[3] = d+'0';}
  64. else if(j==2) {h1 = d - s; x[1] = d+'0';x[2] = '-';x[3] = s+'0';}
  65. else if(j==3) {h1 = d * s; x[1] = d+'0';x[2] = '*';x[3] = s+'0';}
  66. else if(j==4 && s!=0) {h1 =1.0*d/s;x[1] = d+'0';x[2] = '/';x[3] = s+'0';}
  67. else if(j==5) {h1 =1.0*s/d;x[1] = s+'0';x[2] = '/';x[3] = d+'0';}
  68. else continue;
  69. for (int i = 0; i < 6; i++)
  70. {
  71. if(i==0) {h2 = f + g; y[1] = f+'0';y[2] = '+';y[3] = g+'0'; }
  72. else if(i==1) {h2 = f - g; y[1] = f+'0';y[2] = '-';y[3] = g+'0'; }
  73. else if(i==2) {h2 = g - f; y[1] = g+'0';y[2] = '-';y[3] = f+'0'; }
  74. else if(i==3) {h2 = g * f; y[1] = g+'0';y[2] = '*';y[3] = f+'0'; }
  75. else if(i==4 && f!=0) {h2 =1.0*g/f;y[1] = g+'0';y[2] = '/';y[3] = f+'0';}
  76. else if(i==5) {h2 =1.0*f/g;y[1] = f+'0';y[2] = '/';x[3] = g+'0';}
  77. else continue;
  78. for (int k = 0; k < 6; k++)
  79. {
  80. if(k==0)ans = h1 + h2;
  81. else if(k==1)ans = h1 - h2;
  82. else if(k==2)ans = h2 - h1;
  83. else if(k==3)ans = h1 * h2;
  84. else if(k==4 && h1!=0)ans = h2/h1;
  85. else if(k==5 && h2!=0)ans = h1/h2;
  86. if(fabs(ans-24.0)<eps){
  87. printf("一种可以的计算方案:\t");
  88. switch (k)
  89. {
  90. case(0):print(x);printf("+");print(y);break;
  91. case(1):print(x);printf("-");print(y);break;
  92. case(3):print(x);printf("*");print(y);break;
  93. case(5):print(x);printf("/");print(y);break;
  94. case(4):print(y);printf("/");print(x);break;
  95. case(2):print(y);printf("-");print(x);break;
  96. default:
  97. break;
  98. }
  99. printf("\n");
  100. flag = 1;
  101. return 1;
  102. }
  103. }
  104. }
  105. }
  106. if(flag)return 1;
  107. return 0;
  108. }
  109. queue<node>Q;
  110. int main(){
  111. while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF){
  112. node st;
  113. while(!Q.empty())Q.pop();
  114. for (int i = 0; i < 4; i++)
  115. {
  116. st.ans = a[i];
  117. st.State = 1<<i;
  118. st.p = 0;
  119. st.op[0] = 7;
  120. st.ch[0] = a[i];
  121. Q.push(st);
  122. }
  123. node nows,next;
  124. int flag = 0;
  125. while(!Q.empty()){
  126. nows = Q.front();
  127. if(fabs(nows.ans-24.0) < eps && nows.State == 15){
  128. flag = 1;
  129. output(nows);
  130. break;
  131. }
  132. for (int i = 0; i < 4; i++)
  133. {
  134. if((nows.State & (1<<i)) == 0)
  135. {
  136. next.State = nows.State | (1<<i);
  137. for (int j = 0; j < 6; j++)
  138. {
  139. if(j==0)next.ans = nows.ans + a[i];
  140. else if(j==1)next.ans = nows.ans - a[i];
  141. else if(j==2)next.ans = a[i] - nows.ans;
  142. else if(j==3)next.ans = a[i] * nows.ans;
  143. else if(j==4 && nows.ans!=0)next.ans = a[i]/nows.ans;
  144. else if(j==5)next.ans = nows.ans/a[i];
  145. else continue;
  146. next.p = nows.p + 1;
  147. for (int k = 0; k < next.p; k++){
  148. next.op[k] = nows.op[k];
  149. next.ch[k] = nows.ch[k];
  150. }
  151. next.ch[next.p] = a[i];
  152. next.op[next.p] = j;
  153. Q.push(next);
  154. }
  155. }
  156. }
  157. Q.pop();
  158. }
  159. if(flag) /*printf("yes\n")*/;
  160. else{
  161. //多1种情况 :某两个操作得到一个结果,另两个操作得到一个结果,二者组合得到一个结果。
  162. //如 12,12,12,10
  163. if(check(a[0],a[1],a[2],a[3])||check(a[0],a[2],a[1],a[3])||check(a[0],a[3],a[1],a[2]))flag = 1;
  164. if(flag)/*printf("yes\n")*/;
  165. else printf("no\n");
  166. }
  167. }
  168. return 0;
  169. }




一些数据:

2 5 5 10

3 3 7 7

4 4 7 7

3 7 9 13

6 9 9 10

12 12 12 10

9 11 12 12

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/64318
推荐阅读
相关标签
  

闽ICP备14008679号