Calculate 24 games with PHP

xiaoxiao2021-03-06  14

Seeking: Three 5, a 1 composition of mathematical expressions = 24, Results: (5-1 / 5) * 5 = 24

Set_time_limit (0); $ VALUES = Array (1, 5, 5, 5); $ Result = 24;

$ list = array ();

Echo "

"; makevalue ($ values); Print_R ($ list);

Function makevalue ($ VALUES, $ set = array ()) {$ Words = Array (" ", "-", "*", "/"); if (sizeof ($ values) == 1) {$ SET [] = array_shift ($ VALUES); Return Makespecial ($ set);} foreach ($ value) {$ tmpvalues ​​= $ values; unset ($ TMPVALUES [$ key]); foreach ($ Words AS $ Word) {MakeValue ($ TMPVALUES, Array_Merge ($ Set, Array, $ Word));}}} Function Makespecial ($ SET) {$ SIZE = SIZEOF ($ SET);

IF ($ SIZE <= 3 ||! in_Array ("/", $ set) &&! in_Array ("*", $ set) {return manetesult ($ set);

For ($ LEN = 3; $ LEN <$ size-1; $ g = 2) {for ($ start = 0; $ start <$ size-1; $ start = 2) {if (! ($ set [$ START-1] == "*" || $ set [$ start-1] == "/" | | $ set [$ start $ g] == "*" || $ set [$ start $ g] = = "/")) Continue; $ SUBSET = Array_SLICE ($ SET, $ Start, $ LEN); if (! In_Array (" ", $ Subset &&! In_Array ("-", $ Subset) Continue; $ Tmpset = $ set; Array_Splice ($ TMPSET, $ Start, $ LEN-1); $ TMPSET [$ START] = "(" .Implode (", $ subset);") "; makespecial ($ tmpset); }

Function Makeresult ($ SET) {Global $ Result, $ list; $ Str = Implode ("", $ set); @EVAL ("/ $ Num = $ Str;"); if ($ Num == $ Result &&! In_Array ($ STR, $ LIST)) $ list [] = $ STR;

?>

转载请注明原文地址:https://www.9cbs.com/read-48191.html

New Post(0)