//一位师兄的算法
#include <iostream>
#include <string>
#include <cmath>using namespace std;const double PRECISION = 1E-6;
const int COUNT_OF_NUMBER  = 4;
const int NUMBER_TO_BE_CAL = 24;double number[COUNT_OF_NUMBER];
string expression[COUNT_OF_NUMBER];bool Search(int n)
{
    if (n == 1) {
        if ( fabs(number[0] - NUMBER_TO_BE_CAL) < 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 ( Search(n - 1) ) return true;
            
            expression[i] = '(' + expa + '-' + expb + ')';
            number[i] = a - b;
            if ( Search(n - 1) ) return true;
            
            expression[i] = '(' + expb + '-' + expa + ')';
            number[i] = b - a;
            if ( Search(n - 1) ) return true;
                                    expression[i] = '(' + expa + '*' + expb + ')';
            number[i] = a * b;
            if ( Search(n - 1) ) return true;            if (b != 0) {
                expression[i] = '(' + expa + '/' + expb + ')';
                number[i] = a / b;
                if ( Search(n - 1) ) return true;
            } 
            if (a != 0) {
                expression[i] = '(' + expb + '/' + expa + ')';
                number[i] = b / a;
                if ( Search(n - 1) ) return true;
            }            number[i] = a;
            number[j] = b;
            expression[i] = expa;
            expression[j] = expb;
        }
    }
    return false;
}void main()
{
    for (int i = 0; i < COUNT_OF_NUMBER; i++) {
        char buffer[20];
        int  x;
        cin >> x;
        number[i] = x;
        itoa(x, buffer, 10);
        expression[i] = buffer;
    }    if ( Search(COUNT_OF_NUMBER) ) {
        cout << "Success." << endl;
    } else {
        cout << "Fail." << endl;
    }        
}

解决方案 »

  1.   

    一个java写的,不过不是我的import java.util.Random;
    import java.util.Stack;public class NumPuzzle{
    private static int n=6;
    private int MAX = 100;
    private int num[] = new int[n];
    private Random rand = new Random();
    private static Stack stk = new Stack();
    public static final int OP_PLUS = 1;
    public static final int OP_MINUS = 2;
    public static final int OP_MULTI = 3;
    public static final int OP_DIVIDE = 4; public static void main(String args[])
    {
    NumPuzzle np = new NumPuzzle();
    np.init();
    if(np.resolve(0, 0, 0))
    outputResult();
    }
    public static void outputResult()
    {
    System.out.println("\nFound!");
    for(int i=0; i<=n; i++)
    System.out.print(stk.pop());
    }
    public void init()
    {
    int i;
    for(i=0; i<n; i++){
    num[i]=(int)(rand.nextFloat()*100);
    System.out.print("   " + num[i]);
    }
    }
    public boolean resolve(int operation, int value, int step)
    {
    if(step!=0){//if it's first step, skip these codes
    if(step>=n) //reach end of num[]
    if(value==24){
    pushElement(-1, value, step);
    return true;//found
    }else
    return false;//not found

    switch(operation){
    case OP_PLUS:
    value += num[step];
    break;
    case OP_MINUS:
    value -= num[step];
    break;
    case OP_MULTI:
    value *= num[step];
    break;
    case OP_DIVIDE:
    if(num[step]==0) {step--; return false;}
    value = Math.round(value/num[step]);
    break;
    }
    }else{//first step, so set initial value
    value = num[0];
    }

    step++;
    if(resolve(OP_PLUS, value, step)) {pushElement(OP_PLUS, value, step); step--; return true;}
    if(resolve(OP_MINUS, value, step)) {pushElement(OP_MINUS, value, step); step--; return true;}
    if(resolve(OP_MULTI, value, step)) {pushElement(OP_MULTI, value, step); step--; return true;}
    if(resolve(OP_DIVIDE, value, step)) {pushElement(OP_DIVIDE, value, step); step--; return true;}
    step--;
    return false;
    }
    public void pushElement(int i, int value, int step)
    { step--;
    switch(i){
    case OP_PLUS:stk.push("" + num[step] + (step==n-1 ? " = " : "  +  "));break;
    case OP_MINUS:stk.push("" + num[step] + (step==n-1 ? " = " : "  -  "));break;
    case OP_MULTI:stk.push("" + num[step] + (step==n-1 ? " = " : "  *  "));break;
    case OP_DIVIDE:stk.push("" + num[step] + (step==n-1 ? " = " : "  /  "));break;
    default:stk.push(" " + value);break;
    }
    }
    }
      

  2.   

    我的JAVA不好,请帮忙加上注释好吗?
    非常感谢……
      

  3.   

    呵呵,要是Java连这个都实现不了,回家卖红薯了。