• 您的位置我爱Aspx >> VC.Net >> 24点游戏的完全解决
  • 24点游戏的完全解决

  • 作者:aspxer  来源:internet  日期:2007-5-21 23:52:29  关键字:
  • 二进制的那种,每次的余数就是操作符号的顺序数字减十(因为,为了把数字和符号同存在一个数组里,我定符号从10开始。) void writeRecord(int num)4。 不足之处: A.还没有找出取不同数字的简单算法,各位有主意告诉我。(就是 inputNumber里产生不重复数字,即不产生 2,2,3,4的组合。) B.输出结果能否排列,或剔除相似的结果。(Forget it, it is too hard!) C.输入数字个数改为3, 或者5等等,实际上我的程序是可以的,可是不知道那里有小虫子,还没有找到。 5. 输出的结果。 #include <iostream> #include <fstream> #include <cmath>using namespace std;const int Number = 4; const int OpNum = 6; const int SignNum =4;enum Op {ADD=10, MIN=11, MUL=12, DIV=13, LEFT=14, RIGHT=15};int OpCount = 0; int NumCount = 0;char *OpStr[OpNum]= {"Addition", "Minus", "Multiply", "Divide", "Left", "Right"};char *OpSign[OpNum] = {"+", "-", "x", "/", "(", ")"};int Operand[Number] = {0};int Operator[Number*3]= {0};void initialize();void display(bool succeed, ofstream& f);bool inputNumber();bool findSolution(ofstream& f);double Add(double , double); double Mul(double , double); double Min(double , double); double Div(double , double);double (*Operation[4])(double , double)= {Add, Min, Mul, Div};int main() { int temp=0, times=0; initialize(); ofstream f; f.open("c:\\game24.txt"); while (inputNumber()) { if (findSolution(f)) { //display(true, f); //temp++; ; } else ; times++; //display(false); //in order to see clearly, omit the no solution display } cout<<"\nTotal "<<NumCount<<" solutions\n"; cout<<"\nAnd total number is"<<times<<endl; f<<"\nTotal "<<NumCount<<" solutions out of "<<times<<" combinations\n";return 0; } double Add(double num1, double num2) { return num1 + num2; }double Mul(double num1, double num2) { return num1 * num2; }double Min(double num1, double num2) { return num1 -num2; }double Div(double num1, double num2) { return num1/num2; }bool findSolution(ofstream& f) { void writeRecord(int num);double total[1000] = {0}; int first=0, second=0, last=0, now=0; bool result = false;for (int i=0; i< SignNum; i++) { total[i] = Operation[i](Operand[0], Operand[1]); } for (int count = 0; count< Number-2; count++) { last += (count==0?0:pow(SignNum,count)); now += pow(SignNum, count+1); second = now; for (int first = last; first< now; first++) { for (int j= ADD; j<=DIV; j++) //control op { total[second] = Operation[j -10](total[first], Operand[count+2]); if ((count==Number-3)&&fabs(total[second]-24)<0.01) { writeRecord(second); display(true, f); result = true; NumCount++; } second++; } } } return result; }void writeRecord(int num) { int pos=num; for (int i=1; i< Number-1; i++) { pos -= pow(SignNum, i); }for (i=Number -2; i>=0; i--) { Operator[i*2+1]= pos%SignNum+10; //from back to front, and pos = pos/SignNum; } } void initialize() { for (int i=0; i< Number; i++) { Operand[i] = 1; } Operand[0] = 0; //this is ugly as I need to add 1 at each time,so make it zero to add 1 } bool inputNumber() { int i=0; while (true) { if (Operand[i]< 9) //increase one by one number { Operand[i] ++; return true; } else { if (i==Number -1) break; Operand[i] = 1; //this is something like "进位" i++; } } return false; } void display(bool succeed, ofstream& f) { void addNumber(); void addParenthesis(); void displaySolution(ofstream& f);if (succeed) { addNumber(); addParenthesis(); displaySolution(f); } else { cout<<"\nNo solution for these numbers:\n"; for (int i=0; i< Number; i++) { cout<<Operand[i]<<´\t´; } cout<<endl; } } void displaySolution(ofstream& f) { cout<<"\nThe solution is:\n"; for (int i =0; i< OpCount; i++) { if (Operator[i]< 10) { cout<<Operator[i]; f<<Operator[i]; } else { cout<<" "<<OpSign[Operator[i] - 10]<<" "; f<<" "<<OpSign[Operator[i] - 10]<<" "; } } f<<´\n´; }void addNumber() { for (int i=0; i< Number; i++) { Operator[i*2] = Operand[i]; } OpCount = Number * 2 - 1; }void addParenthesis() { void insertParenthesis(int pos);bool findLow= false, findHigh= false;for (int i =0; i <OpCount; i++) { if (Operator[i]==11||Operator[i]==10) //add, min findLow = true; else { if (Operator[i]==12||Operator[i]==13) //mul, div { if (findLow) //only find add, min then findHigh is meaningful findHigh = true; } } if (findLow&&findHigh) { insertParenthesis(i); findLow = false; findHigh = false; } } }void insertParenthesis(int pos) { void insert(int pos, int item); insert(pos, RIGHT); //Sequence is important due to pos++ insert(0, LEFT);}void insert(int start, int item) { int hold1, hold2;hold1 = Operator[start]; hold2 = Operator[start+1];for (int i = start; i< OpCount; i++) { Operator[i + 1] = hold1;hold1 = hold2; hold2 = Operator[i+2]; } Operator[start] = item; OpCount++; } A glimpse of the output in screen. Click to maximize and here is the total again. 8 x 3 + 1 - 1

     

    我对这篇文章有话说?
  • 广告位招租,广告代号:content_468_15
  • 上一篇:没有了!
    下一篇:集群式数字监控应用模型研究(一)
  • 相关文章