题目: |
program test_2011_4;
var
i, j, k, x, n, ans, s: integer;
a:array[1..500] of integer;
f:boolean;
begin
readln (n, x);
for i:=1 to n do read(a[i]);
i:=1;j:=n;f:=false;s:=0;
while (i<=j) and not f do
begin
k:=trunc((i+j)/2);s:=s+1;
if x=a[k] then
begin
ans:=k;f:=true;
end
else if x<a[k] then j:=k-1 else i:=k+1; end;
if f then write(ans,’ ’,s) else write(s);
end.
(1) 输入:
4 11
7 9 11 17
(2) 输入:
20 256
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 101 105 109 110 138 输出:3 2|5
|