Php digital game Computing 24 algorithm

Source: Internet
Author: User
Enter any four numbers and then combine them with +-*. the obtained mathematical expression value equals to 24. The idea is to regard each number as an independent mathematical expression, the expression is combined with punctuation marks to form a new expression, which is combined four times in total. all the combinations between expressions can be implemented through recursion.
The code is as follows:
The code is as follows:
/**
* A 24 maker
* @ Version 1.0.0
* @ Author laruence
* @ Copyright (c) 2009 http://www.laruence.com
*/
Class TwentyFourCal {
Public $ needle = 24;
Public $ precision = '1E-6 ';
Function TwentyFourCal (){
}
Private function notice ($ mesg ){
Var_dump ($ mesg );
}
/**
* Obtain user input methods
*/
Public function calculate ($ operants = array ()){
Try {
$ This-> search ($ operants, 4 );
} Catch (Exception $ e ){
$ This-> notice ($ e-> getMessage ());
Return;
}
$ This-> notice ('Can \'t compute! ');
Return;
}
/**
* Seek the 24-point algorithm PHP implementation
*/
Private function search ($ expressions, $ level ){
If ($ level = 1 ){
$ Result = 'Return '. $ expressions [0].';
If (abs (eval ($ result)-$ this-> needle) <= $ this-> precision ){
Throw new Exception ($ expressions [0]);
}
}
For ($ I = 0; $ I <$ level; $ I ++ ){
For ($ j = $ I + 1; $ j <$ level; $ j ++ ){
$ ExpLeft = $ expressions [$ I];
$ ExpRight = $ expressions [$ j];
$ Expressions [$ j] = $ expressions [$ level-1];
$ Expressions [$ I] = '('. $ expLeft. '+'. $ expRight .')';
$ This-> search ($ expressions, $ level-1 );
$ Expressions [$ I] = '('. $ expLeft. '*'. $ expRight .')';
$ This-> search ($ expressions, $ level-1 );
$ Expressions [$ I] = '('. $ expLeft. '-'. $ expRight .')';
$ This-> search ($ expressions, $ level-1 );
$ Expressions [$ I] = '('. $ expRight. '-'. $ expLeft .')';
$ This-> search ($ expressions, $ level-1 );
If ($ expLeft! = 0 ){
$ Expressions [$ I] = '('. $ expRight. '/'. $ expLeft .')';
$ This-> search ($ expressions, $ level-1 );
}
If ($ expRight! = 0 ){
$ Expressions [$ I] = '('. $ expLeft. '/'. $ expRight .')';
$ This-> search ($ expressions, $ level-1 );
}
$ Expressions [$ I] = $ expLeft;
$ Expressions [$ j] = $ expRight;
}
}
Return false;
}
Function _ destruct (){
}
}
/* Demo */
$ Tf = new TwentyFourCal ();
$ Tf-> calculate (array (4, 8, 8 ));
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.