Php digital game Computing 24 algorithm _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php digital game Computing 24 algorithm. Algorithm idea: think of every number as an independent mathematical expression. the expression is combined with punctuation to form a new expression. it is combined four times in total. all the combinations between expressions can be considered as an algorithm idea: each number is considered as an independent mathematical expression. a punctuation symbol is added between expressions to form 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:

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 ));
?>

...

Related Article

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.