Many people have written some interesting algorithms when learning C language. In fact, these algorithms can also be implemented in PHP, and the codes of some algorithms are even simpler than those in C language.
To learn more about PHP, you can click us
1. A group of monkeys line up in a circle, press 1, 2 ,...,n are numbered in sequence. Then start counting from the 1st one, count to the mth one, kick it out of the circle, start counting from behind it, count to the mth one, kick it out..., and continue in this way until the end. Until there is only one monkey left, that monkey is called the king. Programming is required to simulate this process, input m, n, and output the number of the last king.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function king( $n , $m ){
$monkeys = range(1, $n );
$i =0;
while ( count ( $monkeys )>1) {
if (( $i +1)% $m ==0) {
unset( $monkeys [ $i ]);
} else {
array_push ( $monkeys , $monkeys [ $i ]);
unset( $monkeys [ $i ]);
}
$i ++;
}
return current( $monkeys );
}
echo king(6,3);
|
Copy after login
#2. There is a cow that can give birth to children at the age of 4. One cow is born every year. All the offspring are the same cow. She is sterilized at the age of 15 and can no longer bear children. 20 If a person dies at the age of n years, how many cows will he have after n years?
1 2 3 4 5 6 7 8 9 10 11 12 | function niu( $y ){
static $num = 1;
for ( $i =1; $i <= $y ; $i ++) {
if ( $i >=4 && $i <15){
$num ++;
niu( $y - $i );
} else if ( $i ==20){
$num --;
}
return $num ;
}
}
|
Copy after login
3. Yang Hui Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php
class T{
private $num ;
public function __construct( $var =10) {
if ( $var <3) die ( "值太小啦!" );
$this ->num= $var ;
}
public function display(){
$n = $this ->num;
$arr = array ();
$arr [1]= array_fill (0,3,0);
$arr [1][1]=1;
echo str_pad ( " " , $n *12, " " );
printf( "%3d" , $arr [1][1]);
echo "<br/>" ;
for ( $i =2; $i <= $n ; $i ++){
$arr [ $i ]= array_fill (0,( $i +2),0);
for ( $j =1; $j <= $i ; $j ++){
if ( $j ==1)
echo str_pad ( " " ,( $n +1- $i )*12, " " );
printf( "%3d" , $arr [ $i ][ $j ]= $arr [ $i -1][ $j -1]+ $arr [ $i -1][ $j ]);
echo " " ;
}
echo "<br/>" ;
}
}
}
$yh = new T('3');
$yh ->display();
?>
|
Copy after login
4. Bubble sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function maopao( $arr ){
$len = count ( $arr );
for ( $k =0; $k <= $len ; $k ++)
{
for ( $j = $len -1; $j > $k ; $j --){
if ( $arr [ $j ]< $arr [ $j -1]){
$temp = $arr [ $j ];
$arr [ $j ] = $arr [ $j -1];
$arr [ $j -1] = $temp ;
}
}
}
return $arr ;
}
|
Copy after login
5.Quick sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function quickSort( $arr ) {
$length = count ( $arr );
if ( $length <= 1) {
return $arr ;
}
$base_num = $arr [0];
$left_array = array ();
$right_array = array ();
for ( $i =1; $i < $length ; $i ++) {
if ( $base_num > $arr [ $i ]) {
$left_array [] = $arr [ $i ];
} else {
$right_array [] = $arr [ $i ];
}
}
$left_array = quickSort( $left_array );
$right_array = quickSort( $right_array );
return array_merge ( $left_array , array ( $base_num ), $right_array );
}
|
Copy after login
6.Binary search algorithm (half search algorithm)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function binsearch( $x , $a ){
$c = count ( $a );
$lower =0;
$high = $c -1;
while ( $lower <= $high ){
$middle = intval (( $lower + $high )/2);
if ( $a [ $middle ]> $x ){
$high = $middle -1;
} elseif ( $a [ $middle ]< $x ){
$lower = $middle +1;
} else {
return $middle ;
}
}
return false;
}
|
Copy after login
7.PHP singular algorithm
1 2 3 4 5 6 7 | <?php
function test(){
$a =1;
$b =& $a ;
echo (++ $a )+(++ $a );
}
test();
|
Copy after login
Versions below PHP7 return 6, and versions below PHP7 return 5, which is really strange. Personally, the underlying algorithm is poor, and I think it is a BUG in versions below PHP7
8. Character set: Enter a string, find the character set contained in the string, and sort it in order (English)
1 2 3 4 5 6 7 8 9 10 | function set( $str ){
$arr = str_split ( $str );
$arr = array_flip ( array_flip ( $arr ));
sort( $arr );
return implode('', $arr );
}
|
Copy after login
9. Traverse the characters under a file All files and files under subfolders
1 2 3 4 5 6 7 8 9 10 11 12 13 | function AllFile( $dir ){
if ( $dh = opendir( $dir )){
while (( $file = readdir( $dh )) !== false){
if ( $file !='..' && $file !='.'){
if ( is_dir ( $dir .'/'. $file )){
AllFile( $dir .'/'. $file );
} else {
echo $file ;
}
}
}
}
}
|
Copy after login
10. Extract the file extension from a standard Url
1 2 3 4 5 6 7 | function getExt( $url )
{
$arr = parse_url ( $url );
$file = basename ( $arr ['path']);
$ext = explode ('.', $file );
return $ext [ count ( $ext )-1];
}
|
Copy after login
11. Yes A person wants to go up n steps, but he can only take 1 or 2 steps at a time. Question: How many ways can this person complete the step? For example: There are 3 steps in total. You can take step 1 first and then step 2, or step 2 steps first and then step 1, or step 1 level 3 times for a total of 3 ways
1 2 3 | function jieti( $num ){
return $num <2?1:jieti( $num -1)+jieti( $num -2);
}
|
Copy after login
12. Please write a piece of PHP code to ensure that multiple processes can successfully write to the same file at the same time
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
$fp = fopen ( "lock.txt" , "w+" );
if ( flock ( $fp ,LOCK_EX)) {
fwrite( $fp , "write something" );
flock ( $fp , LOCK_UN);
} else {
echo "file is locking..." ;
}
fclose( $fp );
?>
|
Copy after login
13. Unlimited classification
1 2 3 4 5 6 7 8 9 10 11 12 | function tree( $arr , $pid =0, $level =0){
static $list = array ();
foreach ( $arr as $v ) {
if ( $v ['pid'] == $pid ) {
$v ['level'] = $level ;
$list [] = $v ;
tree( $arr , $v ['id'], $level +1);
}
}
return $list ;
}
|
Copy after login
14. Get the first and last day of last month
1 2 3 4 5 | date ('Y-m-01', strtotime ('-1 month'));
date ('Y-m-t', strtotime ('-1 month'));
|
Copy after login
15. Randomly input a number to query the corresponding data interval
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function binsearch( $x , $a ){
$c = count ( $a );
$lower =0;
$high = $c -1;
while ( $lower <= $high ){
$middle = intval (( $lower + $high )/2);
if ( $a [ $middle ]>= $x ){
$high = $middle -1;
} elseif ( $a [ $middle ]<= $x ){
$lower = $middle +1;
}
}
return '在区间'. $a [ $high ].'到'. $a [ $lower ];
}
$array = ['1','50','100','150','200','250','300'];
$a = '120';
echo binsearch( $a , $array );
|
Copy after login
The above is the detailed content of PHP classic interesting algorithm. For more information, please follow other related articles on the PHP Chinese website!