For example:
The first number is: 1.
Looking at the first number you can say 1, then the second number is: 11.
Looking at the second number you can say 2 1s, that is, the third number is: 21.
Looking at the third number, you can say 1 2 and 1 1, that is, the fourth number is: 1211.
Looking at the fourth number, you can say 1 1, 1 2, and 2 1s, that is, the fifth number is: 111221.
…………
For detailed instructions, please see: http://en.wikipedia.org/wiki/Look-and-say_sequence
The following uses PHP to implement this sequence, as follows:
Copy code The code is as follows:
function look($str)
{
$len = strlen($str);
$count=0;
$result='';
$temp=$str[0];
for($i=0;$i<$len;$i++)
{
if($temp!=$str[$i])
{
$result.=$count.$temp;
$temp = $str[$i] ;
$count=1;
}
else
{
$count++;
}
}
$result.=$count.$temp;
return $result;
}
$test_str = "1";
echo $test_str.'';
for($i=0;$i< 10;$i++)
{
$test_str=look($test_str);
print $test_str."";
}
Note In the for loop in the look function, when $len-1, $result does not accumulate the statistical results of the last digit, so it accumulates again after the loop is completed.
Final output:
1
11
21
1211
111221
312211
13112221
1113213211
31131211 131221
13211311123113112211
11131221133112132113212221
Author: ywxgod
http://www.bkjia.com/PHPjc/323511.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323511.htmlTechArticleFor example: The first number is: 1. Looking at the first number you can say 1, then the second number is: 11. Looking at the second number you can say 2 1's, which means the third number is: 21. ...