#!/bin/bash
#program:calulate the times to find "k" in 1-n
#history:2016-2-26 MissZhou first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function find()
{
declare -i low=1
declare -i high=$(($1))
declare -i mid
declare -i res=0
declare -i fn=$(($2))
while [ $low -lt $high ]
do
mid=$(($high/2+$low/2))
res=$(($res+1))
if [ $mid -lt $fn ] ; then
low=$(($mid+1))
else
high=$(($mid-1))
fi
echo "$mid";
done
echo "find $res times"
}
read -p "input range " n
read -p "input found " k
find n k
一晚上的成果~所以说,新学一门语言还是得自己写例子^_^