Free Pascal IDE高手来

来源:百度知道 编辑:UC知道 时间:2024/05/30 18:16:58
折半查找?~~~~谁会???注意一下最后一个数,要自己注意

假定由以下15个整数:
12 15 17 24 28 32 45 67 89 145 167 178 211 235 269
查找方法:
1、将待查找的数放在x中
2、先与中间数比较,若相等,则查找到了,转(5)
3、若小于中间数,则到左边分支去查找;
若大于中间数,则到右边分支去查找
4、重复(2)、(3),一直查找到没有分支可查找为止
5、结束查找,输出查找结果
假定由以下15个整数:
12 15 17 24 28 32 45 67 89 145 167 178 211 235 269
必须先定义啊!!!

这道又叫二分查找
var
a:array[1..10] of integer;
i,j,n,x:integer;
begin
writeln('shurushu');
for i:=1 to 10 do read(a);
writeln('shuruchazhaoshu');
readln(x);
i:=1; n:=10; j:=trunc((i+n)/2);
repeat
if a[j]>x then
begin
n:=j; j:=trunc((i+n)/2)
end
else
begin
i:=j+1; j:=trunc((i+n)/2)
end
until (a[j]=x) or (i=j) ;
if a[j]=x then
writeln(j:3)
else
writeln('meiyou')
end.