码迷,mamicode.com
首页 > 其他好文 > 详细

24点-code1

时间:2014-09-16 23:35:11      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   div   sp   

  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 = 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

标签:style   blog   color   io   os   ar   for   div   sp   

原文地址:http://www.cnblogs.com/leoshuyi/p/3976033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!