码迷,mamicode.com
首页 > 编程语言 > 详细

24点 C/C++代码

时间:2014-05-16 07:12:29      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

 

 

bubuko.com,布布扣
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
const double PRECISION = 1E-6;
const int COUNT = 4;
const int RESULT = 24;
double number[COUNT];     //这里一定要用double,看看第一题的答案就知道为什么了
string expression[COUNT];    //保存表达式

bool Test(int n)
{

//递归结束
    if (n == 1)
    {
      if (fabs(number[0] - RESULT) < PRECISION)
         {
           cout << expression[0] << endl;
           return true;
         }
    else
       return false;
     }

//递归过程

for (int i = 0; i < n; i++)
{

    for (int j = i + 1; j < n; j++)
      {
         double a, b;
         string expa, expb;
         a = number[i];
         b = number[j];
         number[j] = number[n - 1];
         expa = expression[i];
         expb = expression[j];
         expression[j] = expression[n - 1];
         expression[i] = ( + expa + + + expb + );
         number[i] = a + b;
        if (Test(n - 1))
        return true;
       //减号有两种情况,a-b与b-a
       expression[i] = ( + expa + - + expb + );
       number[i] = a - b;
       if (Test(n - 1))
       return true;
       expression[i] = ( + expb + - + expa + );
       number[i] = b - a;
       if (Test(n - 1))
       return true;
       expression[i] = ( + expa + * + expb + );
       number[i] = a * b;
       if (Test(n - 1))
       return true;
        //除法也有两种情况,a/b与b/a
       if (b != 0)
        {
         expression[i] = ( + expa + / + expb + );
         number[i] = a / b;
         if (Test(n - 1))
         return true;
        }
      if (a != 0)
     {
      expression[i] = ( + expb + / + expa + );
      number[i] = b / a;
      if (Test(n - 1))
      return true;
     }
//恢复数组
number[i] = a;
number[j] = b;
expression[i] = expa;
expression[j] = expb;
}
}
return false;
}

int main(void)
{

    for (int i = 0; i < COUNT; i++)
    {   
       char buffer[20];
       int x;
       cin >> x;
       number[i] = x;
       itoa(x, buffer, 10);
       expression[i] = buffer;
     }

     if (Test(COUNT))
      cout << "Success" << endl;
     else
      cout << "Fail" << endl;
     system("pause");
     return 0;
}
bubuko.com,布布扣

 

24点 C/C++代码,布布扣,bubuko.com

24点 C/C++代码

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/persistentlyworking/p/3725962.html

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