残局

留下评论

红方走,红方是否有和棋的可能?

24-point Algorithm

留下评论

#include <iostream>
#include <string.h>
using namespace std;

#define NUM 4

int a[NUM];
int b[NUM – 1];
int c[NUM – 2];

string expression;
int count;

typedef enum {
    ADD,
    MINUS, // a – b
    MINUS1, // b – a
    MULTIPLE,
    DEVIDE, // a / b
    DEVIDE1 // b / a
} Operators;

// n <= 19 && n >= 0
void itoa_2bits(int n, char * s)
{
    if (n >= 10) {
        s[0] = ‘1’;
        s[1] = n – 10 + ‘0’;
    } else {
        s[0] = n + ‘0’;
    }
}

int Compute(int operand1, int operand2, Operators oper)
{
    int result;
    switch (oper) {
        case ADD:
        result = operand1 + operand2;
        break;
        case MINUS:
        result = operand1 – operand2;
        break;
        case MULTIPLE:
        result = operand1 * operand2;
        break;
        case DEVIDE:
        result = operand1 / operand2;
        break;
        case MINUS1:
        result = operand2 – operand1;
        case DEVIDE1:
        result = operand2 / operand1;
        break;
        default:
        result = 0;
        break;
    }
    return result;
}

string ConcatString(int operand1, int operand2, Operators oper)
{
    string s;
    s.append("(");

    char n1[3];
    char n2[3];
    memset(n1, 0, sizeof(char) * 2);
    memset(n2, 0, sizeof(char) * 2);
    itoa_2bits(operand1, n1);
    itoa_2bits(operand2, n2);
    switch (oper) {
        case ADD:
        s.append(n1, 3);
        s.append("+");
        s.append(n2, 3);
        break;
        case MINUS:
        s.append(n1, 3);
        s.append("-");
        s.append(n2, 3);
        break;
        case MULTIPLE:
        s.append(n1, 3);
        s.append("*");
        s.append(n2, 3);
        break;
        case DEVIDE:
        s.append(n1, 3);
        s.append("/");
        s.append(n2, 3);
        break;
        case MINUS1:
        s.append(n2, 3);
        s.append("-");
        s.append(n1, 3);
        break;
        case DEVIDE1:
        s.append(n2, 3);
        s.append("/");
        s.append(n1, 3);
        break;
        default:
        break;
    }
    s.append(")");
    return s;
}

void SolutionFor24(int n)
{
    if (n == 2) {
        for (int i = ADD; i <= DEVIDE1; ++i) {
            if (Compute(c[0], c[1], static_cast<Operators>(i)) == 24) {
                ++count;
                expression.append(c[0], c[1], static_cast<Operators>(i));
            }
        }
    } else {
        // 1. choose two numbers and apply six operators to them as the new number in the set;
        // 2. call SolutionFor24(n – 1);
    }
}

int main()
{
    int i;
    while (1) {
        cout << "Please input the four number:" << endl;
        cin >> a[0];
        if (a[0] == 0) {
            break;
        }
        for (i = 1; i < NUM; ++i) {
            cin >> a[i];
        }
        count = 0;
        SolutionFor24(NUM);
    }
    return 0;
}