<?php
//二分查找
function getNumbers($num){
$arr=array(1,2,3,4,5,6,7,8,9,10);
$low=0;
$high=count($arr)-1;
while ( $low <= $high ) {
$mid = intval($low + $high+1 / 2);
if ($arr [$mid] > $num)
$high = $mid - 1;
else if ($arr [$mid] < $num)
$low = $mid + 1;
else
return $arr [$mid];
}
return -1;
}
$num=$_GET['id'];
$res=getNumbers($num);
echo $res;
//二分查找
function getNumbers($num){
$arr=array(1,2,3,4,5,6,7,8,9,10);
$low=0;
$high=count($arr)-1;
while ( $low <= $high ) {
$mid = intval($low + $high+1 / 2);
if ($arr [$mid] > $num)
$high = $mid - 1;
else if ($arr [$mid] < $num)
$low = $mid + 1;
else
return $arr [$mid];
}
return -1;
}
$num=$_GET['id'];
$res=getNumbers($num);
echo $res;