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

LookAndSay的代码

PHP中文网
Release: 2016-05-25 17:12:17
Original
1326 people have browsed it

跳至

<?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;;
}
Copy after login

2. [代码][PHP]代码

最后的结果:
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template