Home  >  Article  >  Backend Development  >  hotmail outlook Look And Say sequence php implementation code

hotmail outlook Look And Say sequence php implementation code

WBOY
WBOYOriginal
2016-07-29 08:45:121444browse

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 ones, 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 refer to: http://en.wikipedia.org/wiki/Look-and-say_sequence
Use PHP to implement this sequence as follows:

Copy the 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 the for loop in the look function. When $len-1, $result does not accumulate the statistical results of the last digit, so it is accumulated again after the loop is completed.
Final output result:
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123 113112211
11131221133112132113212221
Author: ywxgod

The above introduces the hotmail outlook Look And Say sequence PHP implementation code, including hotmail outlook content. I hope it will be helpful to friends who are interested in PHP tutorials.

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