Home >Backend Development >PHP Tutorial >hotmail outlook Look And Say sequence php implementation code
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." ";
}
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.