poj 2787

来源:互联网 发布:淘宝宝贝倒计时生成器 编辑:程序博客网 时间:2024/06/17 01:42
#include <iostream>#include <cmath>using namespace std;const int NUM = 4;const int TARGET = 24;const double ZERO = 1e-6;bool flag = false;double number[NUM];bool Deal(int);int main(){    while(cin >> number[0])    {        if(fabs(number[0] - 0.0) < ZERO)            break;        for(int i = 1; i < NUM; ++i)            cin >> number[i];        flag = false;        Deal(NUM);        if(flag)            cout << "YES" << endl;        else            cout << "NO" << endl;    }    return 0;}bool Deal(int step){    if(step == 1)    {        if(fabs(number[0] - TARGET) < ZERO)        {            flag = true;            return true;        }        else            return false;    }    for(int i = 0; i < step; ++i)    {        for(int j = i + 1; j < step; ++j)        {            double tmp_a = number[i], tmp_b = number[j];            number[j] = number[step - 1];            number[i] = tmp_a + tmp_b;            Deal(step - 1);            number[i] = tmp_a - tmp_b;            Deal(step - 1);            number[i] = tmp_b - tmp_a;            Deal(step - 1);            number[i] = tmp_a * tmp_b;            Deal(step - 1);            if(fabs(tmp_b - 0.0) > ZERO)            {                number[i] = tmp_a / tmp_b;                Deal(step - 1);            }            if(fabs(tmp_a - 0.0) > ZERO)            {                number[i] = tmp_b / tmp_a;                Deal(step - 1);            }            number[i] = tmp_a;            number[j] = tmp_b;        }    }    return true;}


这道题是参考了别人的代码写的,之前写的那个太麻烦了,简直直逼枚举代码长度

看了之后才发现可以用double的精度,同时调整运算顺序只需一个循环即可~

0 0
原创粉丝点击