• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
AC_LG
博客园    首页    新随笔    联系   管理    订阅  订阅

24点纸牌游戏

有一种游戏叫做24点,首先是拿一副牌,每次抽出4张,然后利用+,-,*,/四种运算,每张牌只能用一次,而且像10/3这种有余数的运算不能算,结果能算出24点吗?(说明:J到K都是算10点,不论花色的)

由于无聊,所以在火车上想起了这种游戏,当我们想不出来时,能不能写个程序来解决呢?这是当时决定写这个程序的原因。

算法:暴力法,想下四张牌放在台面上,然后往牌中间填三个运算符,枚举四张牌和三个运算符的可能组合,牌的组合有4*3*2*1种,运算符有4*4*4中,然后总的枚举次数是1536种

要注意的细节是算符优先级问题,其实只要考虑两种情况就行,首先是因为是枚举牌的组合,所以一是不用考虑优先级,依次由由右往左依次做运算就行,还有一种是前两张先运算,然后是后两张运算,最后是两次运算的结果再做运算就行,下面是自己写的程序,写得不好求评:

View Code
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 int a[4];
  6 char op[] = {'+', '-', '*', '/'};
  7 
  8 int ok(int k1, int k2, int k3, int k4, char op1, char op2, char op3);
  9 int op_num(int aa, int bb, char op_op);
 10 void init_scanf();
 11 int judge(int k1, int k2, int k3, int k4);
 12 
 13 int judge(int k1, int k2, int k3, int k4)
 14 {
 15     int i,j,k;
 16     
 17     for(i = 0; i < 4; i++)
 18     {
 19           for(j = 0; j < 4; j++)
 20           {
 21                 for(k = 0; k < 4; k++)
 22                 {
 23                        if (ok(k1,k2,k3,k4,op[i],op[j],op[k]))
 24                        {
 25                             return 0;
 26                        }
 27                 }
 28           }
 29     }
 30     return 1;
 31 }
 32 
 33 int ok(int k1, int k2, int k3, int k4, char op1, char op2, char op3)
 34 {
 35                int ans, flag = 0;
 36                
 37                ans = op_num(k1, k2, op1);
 38                if(ans == -9999)
 39                       return 0;
 40                ans = op_num(ans, k3, op2);
 41                if(ans == -9999)
 42                       return 0;
 43                ans = op_num(ans, k4, op3);
 44                if(ans == -9999)
 45                       return 0;       
 46                
 47                if(ans == 24)
 48                {
 49                       printf("yes\n");
 50                       printf("((%d%c%d)%c%d)%c%d\n", k1, op1, k2, op2, k3, op3, k4);
 51                       return 1;
 52                }
 53                else
 54                {
 55                    int ans1, ans2;
 56                        ans1 = op_num(k1, k2, op1);
 57                        if(ans1 == -9999)
 58                               return 0;
 59                        ans2 = op_num(k3, k4, op3);
 60                        if(ans2 == -9999)
 61                               return 0;
 62                        ans = op_num(ans1, ans2, op2);
 63                        if(ans == -9999)
 64                               return 0; 
 65                        if(ans == 24)
 66                        {
 67                               printf("yes\n");
 68                               printf("(%d%c%d)%c(%d%c%d)\n", k1, op1, k2, op2, k3, op3, k4);
 69                               return 1;
 70                        }
 71                }
 72                       return 0;
 73 }
 74 
 75 int op_num(int aa, int bb, char op_op)
 76 {
 77     switch(op_op)
 78     {
 79                  case '+': return aa+bb;
 80                  case '-': return aa-bb;
 81                  case '*': return aa*bb;
 82                  case '/': {
 83                       if(bb == 0 ||aa%bb)  return -9999;
 84                       return aa/bb;
 85                  }
 86                  default: return -9999;
 87     }
 88 }
 89 
 90 void init_scanf()
 91 {
 92         int i,j,k,l;
 93         int k1,k2,k3,k4;
 94         for(i = 0; i < 4; i++)
 95                   scanf("%d", a+i);
 96             
 97             for(i = 0; i < 4; i++)
 98             {
 99                   k1 = a[i];
100                   for(j = 0; j < 4; j++)
101                   {
102                         k2 = a[j];
103                         if(j == i)
104                               continue;
105                         else
106                         {
107                             for(k = 0; k < 4; k++)
108                             {
109                                   k3 = a[k];
110                                   if(k == i || k == j)
111                                        continue;
112                                   else
113                                   {
114                                       for(l = 0; l < 4; l++)
115                                       {
116                                             k4 = a[l];
117                                             if(l == i || l == j || l == k)
118                                                  continue;
119                                             else
120                                             {
121                                                 if(judge(k1,k2,k3,k4) == 0)
122                                                    return ;
123                                             }
124                                       }
125                                   }
126                             }
127                         }
128                   }
129             }
130             
131             printf("No\n");
132 }
133 
134 int main(void)
135 {
136 
137     
138     while(1)
139     {
140             printf("please input 4 numbers from 1 to 10 to count 24 points:\n");
141             init_scanf();
142     }
143     system("pause");
144     return 0;
145 }

 

posted @ 2012-06-30 13:59  AC_LG  阅读(1757)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3