首页 > 代码库 > hdu1427 速算24点

hdu1427 速算24点

</pre><pre>
//#pragma comment(linker, "/STACK:102400000,102400000")
//HEAD
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstdlib>

using namespace std;
//LOOP
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//STL
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RS(s) scanf("%s", s)
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1010;


#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)
#define ALL(c) (c).begin(), (c).end()
#define SZ(V) (int)V.size()
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
typedef vector <int> VI;
typedef unsigned long long ULL;
const double eps = 1e-10;
const LL MOD = 1e9 + 7;

int in[10];
int use[10];

int dfs(int n)
{
    if (n == 7)
    {
//        REP(i, 7)   cout << in[i] << ' ';
//    cout << "------------" << endl;
        if (in[6] == 24)
            return 1;
        return 0;
    }
    REP(i, n)
        if (!use[i])
        {
            use[i] = 1;
            FF(j, i + 1, n)
                if (!use[j])
                {
                    use[j] = 1;
                    REP(op, 6)
                    {
                        if (op == 0) in[n] = in[i] + in[j];
                        if (op == 1) in[n] = in[i] - in[j];
                        if (op == 2) in[n] = in[i] * in[j];
                        if (op == 3 && in[j] && in[i] % in[j] == 0) in[n] = in[i] / in[j];
                        if (op == 4) in[n] = in[j] - in[i];
                        if (op == 5 && in[i] && in[j] % in[i] == 0) in[n] = in[j] / in[i];
                        if (dfs(n + 1)) return 1;
                    }
                    use[j] = 0;
                }
            use[i] = 0;
        }
    return 0;
}

int main()
{
    char s[5][5];
    while (~scanf("%s%s%s%s", s[0], s[1], s[2], s[3]))
    {
        REP(i, 4)
        {
            in[i] = s[i][0] - '0';
            if (s[i][0] == 'A') in[i] = 1;
            else if (s[i][0] == 'J') in[i] = 11;
            else if (s[i][0] == 'Q') in[i] = 12;
            else if (s[i][0] == 'K') in[i] = 13;
            else if (s[i][1] == '0') in[i] = 10;
        }
        CLR(use, 0);
        puts(dfs(4)?  "Yes" : "No");
    }
}


运算结果加入数组末尾后继续枚举
当数组中有7个数字之后表示运算完成