Home >php教程 >PHP源码 >LookAndSay的代码

LookAndSay的代码

PHP中文网
PHP中文网Original
2016-05-25 17:12:171329browse

跳至

<?php
function lookandsay($s)
{
	$r = &#39;&#39;;
	$m = $s{0};
	$n = 1;
	for($i=1, $j = strlen($s); $i<$j; $i++){
		if($s{$i} == $m) {
			$n++;
		} else {
			$r .= $n . $m;
			$m = $s{$i};
			$n = 1;
		}
	}
	return $r . $n . $m;
}
for ($i=0, $s=1; $i<10; $i++){
	$s = lookandsay($s);
	echo $s . &#39;
&#39;;
}

                       

           

2. [代码][PHP]代码 

最后的结果:
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211

           

       

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
Previous article:php 最简单的验证码 Next article:PHP创建空文件