Php digital game computing 24 Algorithm

Source: Internet
Author: User

Algorithm idea: regard each number as an independent mathematical expression, and combine the punctuation between expressions into a new expression. The expression is combined four times in total. All the combinations between expressions can be implemented through recursion.
The Code is as follows: Copy codeThe Code is as follows: <? Php
/**
* A 24 maker
* @ Version 1.0.0
* @ Author laruence <laruence at yahoo.com.cn>
* @ 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.