zoukankan      html  css  js  c++  java
  • 24 Point game

    24 Point game

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:5
     
    描述

    There is a game which is called 24 Point game.

    In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression should be 24 .The expression mustn't have any other operator except plus,minus,multiply,divide and the brackets. 

    e.g. If the numbers you are given is "3 3 8 8", you can give "8/(3-8/3)" as an answer. All the numbers should be used and the bracktes can be nested. 

    Your task in this problem is only to judge whether the given numbers can be used to find a expression whose value is the given number。

     
    输入
    The input has multicases and each case contains one line
    The first line of the input is an non-negative integer C(C<=100),which indicates the number of the cases.
    Each line has some integers,the first integer M(0<=M<=5) is the total number of the given numbers to consist the expression,the second integers N(0<=N<=100) is the number which the value of the expression should be.
    Then,the followed M integer is the given numbers. All the given numbers is non-negative and less than 100
    输出
    For each test-cases,output "Yes" if there is an expression which fit all the demands,otherwise output "No" instead.
    样例输入
    2
    4 24 3 3 8 8
    3 24 8 3 3
    样例输出
    Yes
    No
    来源
    经典改编
    上传者
    张云聪
    第一次由于用了vector数组覆盖的方法,太蠢了,大量的空间移动和数据拷贝,TLE
    超时代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN  103
    #define LLL 1000000000
    #define INF 1000000009
    #define eps 0.00000001
    /*
    给一些数字,能否用利用+-*和/ 将这些数字组成一个特定的值,可以利用括号改变求值顺序
    注意到给的数字个数很小 最多5个
    每次将其中两个抽出来运算,然后将结果插入进去
    */
    double aim;
    int n, T;
    vector<double> v,tmp;
    bool f;
    void print(vector<double> &a)
    {
        for (int i = 0; i < a.size(); i++)
        {
            if (i) printf(" ");
            printf("%lf", a[i]);
        }
        cout << endl;
    }
    void dfs(int x)
    {
        if (x == 1)
        {
            //print(tmp);
            double che = tmp[0] - aim;
            if (tmp[0] - aim > -eps&&tmp[0] - aim < eps)
            {
                f = true;
                //cout << "sdaasddddddddddddddddddddddddddddddddddddddddddddddddddddd" << endl;
            }
            return;
        }
        //print(tmp);
        vector<double> h = tmp;
        double a, b, mul, add, sub1, sub2, div1, div2;
        for (int i = 0; i < x; i++)
        {
            for (int j = i + 1; j < x; j++)
            {
                tmp = h;
                a = tmp[i], b = tmp[j];
                mul = a*b, add = a + b;
                sub1 = a - b, sub2 = b - a;
                if (a != 0.0)
                    div1 = b / a;
                else
                    div1 = INF;
                if (b != 0.0)
                    div2 = a / b;
                else
                    div2 = INF;
                tmp.erase(tmp.begin() + i);
                tmp.erase(tmp.begin() + j - 1);
                vector<double> r = tmp;
                for (int i = 0; i <= tmp.size(); i++)
                {
                    tmp.insert(tmp.begin() + i, add);
                    dfs(tmp.size());
                    tmp = r;
                    if (f) return;
                    tmp.insert(tmp.begin() + i, mul);
                    dfs(tmp.size());
                    tmp = r;
                    if (f) return;
                    tmp.insert(tmp.begin() + i, sub1);
                    dfs(tmp.size());
                    tmp = r;
                    if (f) return;
                    if (sub2-sub2>-eps&&sub1-sub2<eps)
                    {
                        tmp.insert(tmp.begin() + i, add);
                        dfs(tmp.size());
                        tmp = r;
                    }
                    if (f) return;
                    tmp.insert(tmp.begin() + i, div1);
                    dfs(tmp.size());
                    tmp = r;
                    if (f) return;
                    if (div1-div2<eps&&div1-div2>-eps)
                    {
                        tmp.insert(tmp.begin() + i, div2);
                        dfs(tmp.size());
                        tmp = r;
                    }
                    if (f) return;
                }
            }
        }
    }
    int main()
    {
        scanf("%d", &T);
        while (T--)
        {
            v.clear();
            scanf("%d%lf", &n, &aim);
            int t;
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &t);
                v.push_back(t);
            }
            f = false;
            tmp = v;
            dfs(n);
            if (f)
                printf("Yes
    ");
            else
                printf("No
    ");
        }
        return 0;
    }
  • 相关阅读:
    第8.13节 Python类中内置方法__repr__详解
    Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?
    Python中使用eval执行下面函数的结果怎么是字符串'10020'?
    第8.12节 Python类中使用__dict__定义实例变量和方法
    ThinkPHP---thinkphp拓展之空操作
    ThinkPHP---TP功能类之邮件
    ThinkPHP---案例--实现知识管理功能
    ThinkPHP---TP功能类之公文管理功能2----------继续完善
    ThinkPHP---TP拓展之获取IP信息
    ThinkPHP---layer插件
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6838045.html
Copyright © 2011-2022 走看看