40分求一小段PHP的代码,大家请进解决办法

WBOY
Release: 2016-06-13 13:47:56
Original
775 people have browsed it

40分求一小段PHP的代码,大家请进
需求:
从a,b这两个数字中随机取一个数字,组成一串10个字符的字符串。

要求:
1、相邻的两个字母不能相同(0除外),如果随机取到相同的2个连续的字母,需要把后面的字母转换为0,直到出现另外一个不同的字母为止,比如循环后abbb这样的结果,需要转换成ab00;
2、不能在循环之外处理重叠的字母,此操作需要在生成字符串的时候完成。

最后的正确结果举例:0a000b00ab,或者abab0000ba



谢谢!

------解决方案--------------------

PHP code
$last = '';
$r = '';
for($i=0; $i0.5 ? 'a' : 'b';
  $r .= $t == $last ? '0' : $t;
  $last = $t;
}
echo $r;
<br><font color="#e78608">------解决方案--------------------</font><br><?php <br /><br>function chkStr($a,$b)<br>{<br>   $str='';<br>   $c='';<br>   $d='';<br>   $arr=array($a,$b);<br>   while(strlen($str)   {<br>       $d=rand(0,1);<br>       $str.=($c==$arr[$d]) ? ("0") : ($arr[$d]);  <br>       $c=($c==$arr[$d]) ? ("0") : ($arr[$d]);     <br>   }  <br>   return $str; <br>}<br><br>echo chkStr(a,b);<br><br>?> <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template