Rumah >pembangunan bahagian belakang >tutorial php >求指教一个区间查值的问题
if($bbb>0&&$bbb<=100){$baifen=100;} elseif($bbb>100&&$bbb<=500){$baifen=500;} elseif($bbb>500&&$bbb<=1000){$baifen=1000;} elseif($bbb>1000&&$bbb<=5000){$baifen=5000;}
$arr[0,100,500,1000,5000];$b[100,500,1000,5000];
function binarySearch($array,$value){ //起始位置,你可以作为函数参数 $low = 0; $high = count($array)-1; while($low <= $high){ $mid = ceil(($low+$high)/2); if($array[$mid] == $value) return $mid; if($array[$mid] < $value){ $low = $mid + 1; } if($array[$mid] > $value){ $high = $mid - 1; } } return -1; }
直接条件比较
if($bbb > 1000) ) $baifen = 5000;elseif($bbb > 500) $baifen = 1000;elseif($bbb > 100) $baifen = 500;elseif($bbb > 0) $baifen = 100;
$dict = array(0 => 100, 100 => 500, 500 => 1000, 1000 => 5000);krsort($dict);foreach($$dict as $k=>$v) if($x > $k) break;echo $v;
直接条件比较
if($bbb > 1000) ) $baifen = 5000;elseif($bbb > 500) $baifen = 1000;elseif($bbb > 100) $baifen = 500;elseif($bbb > 0) $baifen = 100;
$dict = array(0 => 100, 100 => 500, 500 => 1000, 1000 => 5000);krsort($dict);foreach($$dict as $k=>$v) if($x > $k) break;echo $v;