24点求解

搜索24点的组合方案:
(2*(2+2))*3=24
(2+2)*(2+4)=24
2*(2+(2*5))=24
2*((2+2)+8)=24
2+(2*(2+9))=24
2+(2+(2*10))=24
(2+2)*(3+3)=24
2*((2*3)+6)=24
2*((2+3)+7)=24
2+(2*(3+8))=24
((2+2)+4)*3=24
2*((2*4)+4)=24
2+(2+(4*5))=24
2*((2+4)+6)=24
2+(2*(4+7))=24
(2*(2*4))+8=24
2*((2+5)+5)=24
2+(2*(5+6))=24
(2*(2+5))+10=24
(2*(2+6))+8=24
(2+(2*6))+10=24
2*((2*7)-2)=24
(2*(2+7))+6=24
(2+(2*7))+8=24
(2*(2*8))-8=24
(2+2)*(9-3)=24
(2+(2*9))+4=24
2*((2*9)-6)=24
(2+2)*(10-4)=24
2*((2*10)-8)=24
(2+(2+10))+10=24
((2+3)+3)*3=24
2*((3+3)+6)=24
((2*3)-3)*8=24
((2+3)*3)+9=24
((2+3)*4)+4=24
2*((3+4)+5)=24
(2+(3*4))+10=24
2*((3*5)-3)=24
(2*(3*5))-6=24
(2+(3*5))+7=24
(2*(3+5))+8=24
(2+(3*6))+4=24
(2*(3+6))+6=24
2*(3*(7-3))=24
(2*(3+7))+4=24
2*((3*7)-9)=24
2*(3*(8-4))=24
(2*3)+(8+10)=24
(2+(3*9))-5=24
(2*3)+(9+9)=24
(2+(3+9))+10=24
(2*(3+10))-2=24
2*(3*(10-6))=24
2*((4+4)+4)=24
(2+(4*4))+6=24
(2*(4+4))+8=24
(2*(4+5))+6=24
2*((4*5)-8)=24
(2*4)+(6+10)=24
2*(4*(7-4))=24
(2+(4*7))-6=24
(2*4)+(7+9)=24
(2*4)+(8+8)=24
(2+(4+8))+10=24
((2*4)*9)/3=24
(2+4)*(9-5)=24
2*(4*(9-6))=24
(2+(4+9))+9=24
(2*(4+10))-4=24
2*(4*(10-7))=24
((2*5)-2)*3=24
(2*5)+(2*7)=24
2*((5-2)+9)=24
2*((5-3)+10)=24
((2*5)-4)*4=24
(2*5)+(4+10)=24
(2+(5*5))-3=24
(2*(5+5))+4=24
(2+(5/5))*8=24
(2*5)+(5+9)=24
((2*5)-6)*6=24
(2*5)+(6+8)=24
(2*5)+(7+7)=24
((2*5)-7)*8=24
(2+(5+7))+10=24
(2*(5+8))-2=24
(2+(5+8))+9=24
(2*(5+10))-6=24
(2*6)+(2*6)=24
(2*6)+(3+9)=24
((2+6)-4)*6=24
(2*6)+(4+8)=24
(2*6)+(5+7)=24
(2*6)+(6+6)=24
(2+(6/6))*8=24
(2+(6+6))+10=24
(2+(6+7))+9=24
(2+(6+8))+8=24
(2*(6+9))-6=24
((2*6)-9)*8=24
(2+6)*(10-7)=24
(2*(6+10))-8=24
2*((7-2)+7)=24
(2*7)+(3+7)=24
2*((7-3)+8)=24
(2*7)+(5+5)=24
((2*7)-6)*3=24
((2+7)-6)*8=24
(2*(7+7))-4=24
(2+(7+7))+8=24
((2*7)-8)*4=24
(2*(7+9))-8=24
2*(7+(10/2))=24
(2+(7*10))/3=24
(2*(7+10))-10=24
((2+8)*3)-6=24
((2+8)-7)*8=24
((2*8)-8)*3=24
(2*(8+8))-8=24
(2*(8+9))-10=24
((2*9)-2)+8=24
(2*(9-2))+10=24
((2*9)-4)+10=24
2*((9-6)+9)=24
2*((9-7)+10)=24
((2+9)-8)*8=24
(2+(9/9))*8=24
(2*(10-3))+10=24
((2*10)-5)+9=24
2*((10/5)+10)=24
((2*10)-6)+10=24
2*((10-8)+10)=24
(3*(3*3))-3=24
3+(3*(3+4))=24
(3*3)+(3*5)=24
(3*(3+3))+6=24
3*((3/3)+7)=24
((3+3)-3)*8=24
3*((3*4)-4)=24
(3/3)*(4*6)=24
(3+(3*4))+9=24
((3/3)+5)*4=24
(3+(3*5))+6=24
3*((3*5)-7)=24
(3*3)+(5+10)=24
((3*3)-6)*8=24
(3*3)+(6+9)=24
3*((3*6)-10)=24
(3*(3+7))-6=24
(3*3)+(7+8)=24
(3+3)*(8-4)=24
(3*(3+8))-9=24
(3+(3+8))+10=24
(3+3)*(9-5)=24
(3+(3+9))+9=24
3*(3+(10/2))=24
3+(3*(10-3))=24
(3+(3*10))-9=24
3*((4-3)+7)=24
((3+4)*4)-4=24
(3+(4*4))+5=24
(3+(4/4))*6=24
3*((4/4)+7)=24
(3*4)+(4+8)=24
(3*4)+(5+7)=24
(3*4)+(6+6)=24
(3+(4*7))-7=24
(3*(4+7))-9=24
(3+(4+7))+10=24
3*(4*(8-6))=24
(3+(4+8))+9=24
3*((4+9)-5)=24
((3*4)*10)/5=24
3*((4+10)-6)=24
3*(4*(10-8))=24
(3*5)+(4+5)=24
((3+5)-4)*6=24
((3+5)*4)-8=24
(3*(5+5))-6=24
3*((5/5)+7)=24
((3+5)-5)*8=24
(3+(5*6))-9=24
(3+(5+6))+10=24
(3+(5+7))+9=24
(3+(5+8))+8=24
3*((5+10)-7)=24
3*((6/3)+6)=24
((3+6)-5)*6=24
3*((6-5)+7)=24
(3*(6-5))*8=24
(3+(6/6))*6=24
3*((6/6)+7)=24
((3+6)-6)*8=24
(3+(6+6))+9=24
(3+(6+7))+8=24
3*((6+9)-7)=24
3*((6+10)-8)=24
((3*7)-5)+8=24
3*((7-6)+7)=24
(3+(7+7))+7=24
((3+7)-7)*8=24
((3*7)-7)+10=24
3*(7+(8/8))=24
3+((7*9)/3)=24
3*((7+9)-8)=24
3*(7+(9/9))=24
3*((7+10)-9)=24
3*(7+(10/10))=24
(3+(8/8))*6=24
((3*8)+8)-8=24
3*(8*(9-8))=24
((3*8)+9)-9=24
3*(8*(10-9))=24
((3*8)+10)-10=24
3*(9-(3/3))=24
3*(9-(4/4))=24
(3*(9-4))+9=24
3*(9-(5/5))=24
((3*9)+5)-8=24
(3+9)*(6-4)=24
((3*9)+6)-9=24
3*(9-(7/7))=24
(3+9)*(8-6)=24
3*(9-(9/9))=24
3*((9+9)-10)=24
((3+9)*10)/5=24
3*(9-(10/10))=24
((3*10)+4)-10=24
((3+10)-9)*6=24
3*(10-(10/5))=24
(3+(10/10))*6=24
(4-2)*(5+7)=24
(4+(4*4))+4=24
4*((4/4)+5)=24
((4+4)-4)*6=24
(4*(4+4))-8=24
4*((4*4)-10)=24
((4+4)-5)*8=24
(4+(4+6))+10=24
(4+4)*(7-4)=24
4*((4+7)-5)=24
(4+(4*7))-8=24
(4+(4+7))+9=24
4*((4+8)-6)=24
(4+(4+8))+8=24
4+(4*(9-4))=24
(4+4)*(9-6)=24
4+(4*(10-5))=24
(4+4)*(10-7)=24
4*((4+10)-8)=24
4*((5-4)+5)=24
(4*(5-4))*6=24
4*((5/5)+5)=24
((4+5)-5)*6=24
(4-(5/5))*8=24
((4*5)-5)+9=24
(4+(5+5))+10=24
4*(5+(6/6))=24
((4+5)-6)*8=24
(4+(5+6))+9=24
(4+(5*6))-10=24
4*((5+7)-6)=24
4*(5+(7/7))=24
(4+(5+7))+8=24
4*(5+(8/8))=24
4*((5+9)-8)=24
4*(5+(9/9))=24
4*((5+10)-9)=24
4*(5+(10/10))=24
((4*6)+6)-6=24
(4+(6+6))+8=24
4*(6*(7-6))=24
(4+(6+7))+7=24
((4+6)-7)*8=24
((4*6)+8)-8=24
4*(6*(9-8))=24
((4*6)+9)-9=24
4*(6*(10-9))=24
((4*6)+10)-10=24
(4*(7-3))+8=24
4*(7-(5/5))=24
((4*7)+5)-9=24
4+((7-5)*10)=24
((4*7)+6)-10=24
4*(7-(7/7))=24
4*((7+7)-8)=24
((4+7)-8)*8=24
4*((7+8)-9)=24
4*(7-(9/9))=24
4*((7+9)-10)=24
4*(7-(10/10))=24
4+((8-6)*10)=24
(4-(8/8))*8=24
4*((8+8)-10)=24
((4+8)-9)*8=24
((4+8)*10)/5=24
(4*(9-2))-4=24
(4*9)-(4+8)=24
(4*9)-(6+6)=24
(4-(9/9))*8=24
((4+9)-10)*8=24
(4*(10-3))-4=24
4+((10-8)*10)=24
(5*5)-(3/3)=24
(5*5)-(5/5)=24
((5*5)+5)-6=24
(5+(5+5))+9=24
((5+5)-6)*6=24
((5*5)+6)-7=24
(5+(5+6))+8=24
(5+(5+7))+7=24
((5*5)+7)-8=24
(5*5)-(8/8)=24
((5*5)+8)-9=24
(5*5)-(9/9)=24
((5*5)+9)-10=24
(5*5)-(10/10)=24
(5*(6/2))+9=24
(5-(6/6))*6=24
(5+(6+6))+7=24
((5+6)-8)*8=24
((5*6)*8)/10=24
(5*7)-(2+9)=24
(5-(7/7))*6=24
(5+7)*(8-6)=24
(5+7)*(9-7)=24
((5+7)-9)*8=24
((5+7)*10)/5=24
(5+7)*(10-8)=24
(5*8)-(2*8)=24
(5*8)-(8+8)=24
((5+8)-9)*6=24
((5+8)-10)*8=24
(5*(9/3))+9=24
(5*(9-6))+9=24
((5+9)-10)*6=24
(5-(10/5))*8=24
(5*(10-7))+9=24
(5-(10/10))*6=24
((6/3)*7)+10=24
((6-3)*10)-6=24
(6+(6+6))+6=24
6*((6+6)-8)=24
((6*6)*6)/9=24
(6+(6*7))/2=24
6*((6+7)-9)=24
6+(6*(8-5))=24
6*((6+8)-10)=24
((6+6)-9)*8=24
(6*(6+10))/4=24
((6+6)*10)/5=24
6+(6*(10-7))=24
6+((7-5)*9)=24
6*((7+7)-10)=24
(6*7)-(8+10)=24
(6*(7+9))/4=24
(6*7)-(9+9)=24
(6/(8-6))*8=24
6*(8/(9-7))=24
6*(8/(10-8))=24
(6*9)-(5*6)=24
6*(9-(10/2))=24
6+(9*(10-8))=24
(6*10)-(6*6)=24
((6+10)*9)/6=24
((7-5)*7)+10=24
((7-5)*8)+8=24
(7*(8/4))+10=24
(7*(9-7))+10=24
(7*(10/5))+10=24
(7*(10-8))+10=24
(8-2)+(8+10)=24
(8/2)*(9-3)=24
(8-5)*(10-2)=24
((8-6)*8)+8=24
((8+8)*9)/6=24
8+(8*(9-7))=24
8+(8*(10-8))=24
8*(9-(2+4))=24
(8*(9-5))-8=24
8*(9/(9-6))=24
8*(9/(10-7))=24
(8*10)-(7*8)=24
((8*10)-8)/3=24
(9-4)+(9+10)=24
(9-5)+(10+10)=24
9+((9*10)/6)=24
(9+(10/2))+10=24
(10-6)+(10+10)=24
((10-7)*10)-6=24
((10*10)-4)/4=24
在109768种组合中找到392种方案。

要求满足公式:a ? b? c ? d =24

其中, abcd取自扑克2~10,?取自+-*/, 可随意添加括号改变运算顺序和结合方式。

以上是我计算的结果,不知道对不对,百度不到答案。

贴代码:

  1    static void 枚举24点计算游戏()
  2         {
  3             //a ? b ? c ? d = 24
  4             //a,b,c,d 取自扑克(1~13,每个数字有4个相同的牌)
  5             //? 取自+-*/
  6             //中间结果须为自然数。
  7             //结合方式:1,2,3
  8             //((a?b)?c)?d   1,2,3
  9             //(a?b)?(c?d)   1,3,2
 10             //(a?(b?c))?d   2,1,3
 11             //a?((b?c)?d)   2,3,1
 12             //(a?b)?(c?d)   3,1,2 == 1,3,2
 13             //a?(b?(c?d))   3,2,1
 14             Func<int,int,int>[] op = new Func<int,int,int>[4];
 15             op[0] = (x,y)=>x+y;
 16             op[1] = (x,y)=>x-y>0?x-y:-9999;
 17             op[2] = (x,y)=>x*y;
 18             op[3] = (x,y)=>y!=0&&x%y==0?x/y:-9999;
 19             string[] opString = new string[]{"+","-","*","/"};
 20             bool[] set = new bool[11 * 11 * 11 * 11];
 21             int count=0;
 22             int countN = 0;
 23             Console.WriteLine("搜索24点的组合方案:");
 24                     for(int a =2; a<11; a++)
 25                         for (int b = 2; b < 11; b++)
 26                             for (int c = 2; c < 11; c++)
 27                                                 
 28                              for (int d = 2; d < 11; d++)
 29                                {
 30                                next:
 31                                    if (d > 10) continue;
 32                                     for (int k = 0; k < 4; k++)
 33                                         for (int i = 0; i < 4; i++)
 34                                             for (int j = 0; j < 4; j++)
 35                                             {
 36                                                 countN++;
 37                                                 List<int> sor = new List<int>();
 38                                                 sor.Add(a);
 39                                                 sor.Add(b);
 40                                                 sor.Add(c);
 41                                                 sor.Add(d);
 42                                                 sor.Sort();
 43                                                 int magicNumber = sor[0] * 1000 + sor[1] * 100 + sor[2] * 10 + sor[3];
 44                                                 if (set[magicNumber])
 45                                                 {
 46                                                     d++;
 47                                                     goto next;
 48                                                 }
 49                                                 //(a?(b?c))?d   2,1,3
 50                                                 if (op[k](op[i](a, op[j](b, c)), d) == 24)
 51                                                 {
 52                                                     count++;
 53                                                     Console.WriteLine("({0}{1}({2}{3}{4})){5}{6}=24",
 54                                                         a, opString[i], b, opString[j], c, opString[k], d);
 55                                                     set[magicNumber] = true;
 56                                                     d++;
 57                                                     goto next;
 58                                                 }
 59                                                 //a?((b?c)?d)   2,3,1
 60                                                 if (op[i](a, op[k](op[j](b, c), d)) == 24)
 61                                                 {
 62                                                     count++;
 63                                                     Console.WriteLine("{0}{1}(({2}{3}{4}){5}{6})=24",
 64                                                         a, opString[i], b, opString[j], c, opString[k], d);
 65                                                     set[magicNumber] = true;
 66                                                     d++;
 67                                                     goto next;
 68                                                 }
 69                                                 //a?(b?(c?d))   3,2,1
 70                                                 if (op[i](a, op[j](b, op[k](c, d))) == 24)
 71                                                 {
 72                                                     count++;
 73                                                     Console.WriteLine("{0}{1}({2}{3}({4}{5}{6}))=24",
 74                                                         a, opString[i], b, opString[j], c, opString[k], d);
 75                                                     set[magicNumber] = true;
 76                                                     d++;
 77                                                     goto next;
 78                                                 }
 79                                                 //(a?b)?(c?d) 3,1,2 == 1,3,2
 80                                                 if (op[i](op[j](a, b), op[k](c, d)) == 24)
 81                                                 {
 82                                                     count++;
 83                                                     Console.WriteLine("({0}{1}{2}){3}({4}{5}{6})=24",
 84                                                         a, opString[j], b,
 85                                                         opString[i],
 86                                                         c, opString[k], d);
 87                                                     set[magicNumber] = true;
 88                                                     d++;
 89                                                     goto next;
 90                                                 }
 91                                                 //((a?b)?c)?d  1,2,3
 92                                                 if (op[i](op[k](op[j](a, b), c), d) == 24)
 93                                                 {
 94                                                     count++;
 95                                                     Console.WriteLine("(({0}{1}{2}){3}{4}){5}{6}=24",
 96                                                         a, opString[j], b,
 97                                                         opString[k],
 98                                                         c, opString[i], d);
 99                                                     set[magicNumber] = true;
100                                                     d++;
101                                                     goto next;
102                                                 }
103 
104                                             }
105                             }
106             Console.WriteLine("在{0}种组合中找到{1}种方案。",countN, count);
107         }

 

 

           

 

 

 

posted @ 2013-04-05 22:59  诺贝尔  阅读(722)  评论(0编辑  收藏  举报