穷举法求解24点

看到网上更多的是推荐使用递归,但是自己对递归一向有点头疼,所有使用穷举法求解。VS2005下测试通过。

  1 /********************************************
  2 使用穷举法求解24点。
  3 但是仍然有一个问题:对于1*2与2*1,因为顺序的关系,
  4 系统仍然认为其是两个解,此问题尚待解决。
  5 Author:Chris
  6 Date  :2013/06/18
  7 ********************************************/
  8 #include "stdio.h"
  9 int solutions = 0;
 10 double getNumber(int array[],char express)
 11 {
 12     switch(express)
 13     {
 14     case '+':
 15         return array[0] + array[1];
 16     case '-':
 17         return array[0] - array[1];
 18     case '*':
 19         return array[0] * array[1];
 20     case '/':
 21         return array[0] / array[1];
 22     default:
 23         return 0;
 24     }
 25 }
 26 void executeExpress(int leftArray[],int rightArray[])
 27 {
 28     int left;
 29     int right;
 30     char express[5] = {'+','-','*','/'};
 31     int i;
 32     int j;
 33     for(i = 0;i<4;i++)
 34     {
 35         for(j = 0;j<4;j++)
 36         {
 37             left = getNumber(leftArray,express[i]);
 38             right = getNumber(rightArray,express[j]);
 39             if(left<=0||right<=0)
 40             {
 41                 continue;
 42             }
 43             if((left+right)==24)
 44             {
 45                 solutions++;
 46                 printf("(%d%c%d)%c(%d%c%d)\n",leftArray[0],express[i],leftArray[1],'+',rightArray[0],express[j],rightArray[1]);
 47             }
 48             else
 49                 if((left-right)==24)
 50                 {
 51                     solutions++;
 52                     printf("(%d%c%d)%c(%d%c%d)\n",leftArray[0],express[i],leftArray[1],'-',rightArray[0],express[j],rightArray[1]);
 53                 }
 54                 else
 55                 {
 56                     if((left*right)==24)
 57                     {
 58                         solutions++;
 59                         printf("(%d%c%d)%c(%d%c%d)\n",leftArray[0],express[i],leftArray[1],'*',rightArray[0],express[j],rightArray[1]);
 60                     }
 61                     else
 62                         if((left/right)==24)
 63                         {
 64                             solutions++;
 65                             printf("(%d%c%d)%c(%d%c%d)\n",leftArray[0],express[i],leftArray[1],'/',rightArray[0],express[j],rightArray[1]);
 66                         }
 67                 }
 68         }
 69     }
 70 }
 71 void calculateTwentyFourPoints(int data[])
 72 {
 73     int i;
 74     int j;
 75     int k;
 76     int count;
 77     int left[2];
 78     int right[2];
 79     for(i = 0;i<4;i++)
 80     {
 81         for(j = 0;j<4;j++)
 82         {
 83             if(j==i)
 84                 continue;
 85             else
 86             {
 87                 left[0] = data[i];
 88                 left[1] = data[j];
 89                 k = 0;
 90                 count = 0;
 91                 while(k<4)
 92                 {
 93                     if(k!=i&&k!=j)
 94                         {
 95                             right[count] = data[k];
 96                             count++;
 97                         }
 98                     k++;
 99                 }
100                 executeExpress(left,right);
101             }
102         }
103     }
104     if(solutions==0)
105     {
106         printf("No result found.");
107     }
108     printf("Solution numbers:%d\n",solutions);
109 }
 1 int main(void)
 2 {
 3     int data[5];
 4     int i = 0;
 5     printf("Please enter four numbers.\n");
 6     for(;i<4;i++)
 7     {
 8         printf("Enter:");
 9         scanf("%d",&data[i]);
10     }
11     calculateTwentyFourPoints(data);
12     return 0;
13 }

 结果:

原文地址:https://www.cnblogs.com/chrischeng/p/3143491.html