满意答案
ljmwwq
2013.06.21
采纳率:41% 等级:12
已帮助:16285人
绝对正确。。 选我最佳吧亲。。
var
i,j,k,n,right,temp,left:integer;
a:array[0..1000]of integer;
function template(left,right:integer):integer;
var
temp,i,j:integer;
begin
i:=left;j:=right;
while (j>i) do
begin
while (a[i]i) do
j:=j-1;
if (j>i) then begin temp:=a[i];a[i]:=a[j];a[j]:=temp;i:=i+1;end;
while (a[i]i) do
i:=i+1;
if (j>i) then begin temp:=a[i];a[i]:=a[j];a[j]:=temp;j:=j-1;end;
end;
template:=j-left+1;
end;
procedure select(left,right,k:integer);
begin
n:=right-left+1;
if (k>n) or (k<1) then begin writeln('error'); halt;end
else
begin
temp:=template(left,right);
if k=temp then begin writeln(a[temp+left-1]); exit;end
else begin
if k>temp then select(temp+left,right,k-temp)
else select(left,temp+left-2,k);
end;
end;
end;
begin
assign(input,'number.in');
assign(output,'number.out');
reset(input);
rewrite(output);
readln(n,k);
for i:=1 to n do
read(a[i]);
select(1,n,k);
close(input);
close(output);
end.
02分享举报