首页 > 代码库 > 24点-code1

24点-code1

  1 #include <iostream>  2 #include <string>  3 #include <cstdlib>  4 #include <cmath>  5 using namespace std;  6   7 const double Threshold = 1E-6;  8 const int CardsNumber=4;  9 const int ResultValue = http://www.mamicode.com/24; 10 double numbers[CardsNumber]; 11 string result[CardsNumber]; 12  13 bool Points24(int n) 14 { 15     if (n == 1) 16     { 17         if (fabs(numbers[0] - ResultValue) < Threshold) 18         { 19             cout << result[0] << endl; 20             return true; 21         } 22         else 23         { 24             return false; 25         } 26     } 27     for (int i = 0; i < n; i++) 28     { 29         for (int j = i+1; j < n; j++) 30         { 31             double a, b; 32             string expa, expb; 33             a = numbers[i]; 34             b = numbers[j]; 35             numbers[j] = numbers[n - 1]; 36  37             expa = result[i]; 38             expb = result[j]; 39             result[j] = result[n - 1]; 40  41             numbers[i] = a + b; 42             result[i] = "(" + expa + "+" + expb + ")"; 43             if (Points24(n - 1)) 44             { 45                 //cout << result[i]<<endl; 46                 return true; 47             } 48  49             numbers[i] = a - b; 50             result[i] = "(" + expa + "-" + expb + ")"; 51             if (Points24(n - 1)) 52             { 53                 return true; 54             } 55  56             numbers[i] = b - a; 57             result[i] = "(" + expb + "-" + expa + ")"; 58             if (Points24(n - 1)) 59             { 60                 return true; 61             } 62  63             numbers[i] = a * b; 64             result[i] = "(" + expa + "*" + expb + ")"; 65             if (Points24(n - 1)) 66             { 67                 return true; 68             } 69  70             if (b) 71             { 72                 numbers[i] = a/b; 73                 result[i] = "(" + expa + "/" + expb + ")"; 74                 if (Points24(n - 1)) 75                 { 76                     return true; 77                 } 78             } 79             if (a) 80             { 81                 numbers[i] = b / a; 82                 result[i] = "(" + expb + "/" + expa + ")"; 83                 if (Points24(n - 1)) 84                 { 85                     return true; 86                 } 87             } 88             numbers[i] = a; 89             numbers[j] = b; 90             result[i] = expa; 91             result[j] = expb; 92         } 93     } 94     return false; 95  96  97 } 98 int main() 99 {100     int x;101     for (int i = 0; i < CardsNumber; i++)102     {103         cout << "the NO." << i << " number is: ";104         cin >> x;105         numbers[i] = x;106         char buffer[20];107         itoa(x,buffer,10);108         result[i] = buffer;109     }    110     if (Points24(CardsNumber)) 111         cout << "Success" << endl;112     else cout << "Fail" << endl;113     return 0;114 }

 

24点-code1