40 points for a small piece of PHP code, everyone please enter
Demand:
Randomly take a number from the two number of a, B, and make up a string of 10 characters.
Requirements:
1, the adjacent two letters can not be the same (except 0), if randomly taken to the same 2 consecutive letters, you need to convert the following letter to 0, until another different letter, such as the abbb after the result of the cycle, need to convert to ab00;
2. The overlapping letters cannot be processed outside the loop, and this operation needs to be done when the string is generated.
Examples of final and correct results: 0A000B00AB, or ABAB0000BA
Thank you!
------Solution--------------------
PHP Code
$last = '; $r = '; for ($i =0; $i <10; $i + +) { $t = rand (0, 1) >0.5? ' A ': ' B '; $r. = $t = = $last? ' 0 ': $t; $last = $t;} Echo $r;
------Solution--------------------
function Chkstr ($a, $b)
{
$str = ";
$c = ";
$d = ";
$arr =array ($a, $b);
while (strlen ($STR) <10)
{
$d =rand (0,1);
$str. = ($c = = $arr [$d])? ("0"): ($arr [$d]);
$c = ($c = = $arr [$d])? ("0"): ($arr [$d]);
}
}
Echo Chkstr (A, b);
?>