`

php lookandsay

    博客分类:
  • php
php 
阅读更多

<?php
function lookandsay($str){
        $r = '';
        $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/>";
}
?>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics