php lookandsay

巴扎黑
巴扎黑Original
2016-11-10 13:20:591118browse

<?php
function lookandsay($str){
        $r = &#39;&#39;;
        $m = $str[0];
        $n = 1;
        for($i=1; $i<strlen($str); $i++){
                if($str[$i] == $m){//如果这个字符与上个字符相同则数目加1
                        $n++;
                }else{
                        $r .= $n.$m;//反之,把数目和这个字符追加到返回值
                        $m = $str[$i];//赋值下一个字符
                        $n  = 1;
                }
        }
        return $r.$n.$m;
}
for($i = 0,$str = 1; $i < 9; $i++){
        $str = lookandsay($str);
        print $s."<br/>";
}
?>


Statement:
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