有如下程序: Private Sub Command1_Click() Dim k As Integer,m As Integer Dim op As Integer k=4:m=1 p=PPC(k,m):Print op; p=PPC(k.m):Print op End Sub Private Function PPC(a As Integer,b As Integer) Static m As Integer,i As Integer m=0:i=2 i=i+m+1 m=i+a+b PPC=m End Function 程序运行后,输出的结果为
A.4 6
B.6 6
C.8 8
D.10 12
第1题:
D. 冒泡排序
procedure bubble_sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=n downto i+1 do
if a[j]<a[j-1] then swap( a[j],a[j-1]); {每次比较相邻元素的关系}
end;
第2题:
组合的生成(1..n中选取k个数的所有方案)
procedure solve(dep,pre:integer);
var
i:integer;
begin
if dep=k+1 then begin writeln(s);exit; end;
for i:=1 to n do
if (not used[i]) and (i>pre) then begin
s:=s+chr(i+ord(''0''));used[i]:=true;
solve(dep+1,i);
s:=copy(s,1,length(s)-1); used[i]:=false;
end;
end;
第3题:
有如下程序。
Private Sub Command1_Click()
Dim k As Integer, m As Integer
Dim op As Integer
k=4: m=1
op=PPC(k, m) : Print op;
op=PPC(k, m) : Print op
End Sub
Private Function PPC(a As Integer, b As Integer)
Static m As Integer, i As Integer
m=0: i=2
i=i + m + 1
m=i + a + b
PPC=m
End Function
程序运行后,输出的结果为
A.4 6
B.6 6
C.8 8
D.10 12
第4题:
E.堆排序:
procedure sift(i,m:integer);{调整以i为根的子树成为堆,m为结点总数}
var k:integer;
begin
a[0]:=a[i]; k:=2*i;{在完全二叉树中结点i的左孩子为2*i,右孩子为2*i+1}
while k<=m do begin
if (k<m) and (a[k]<a[k+1]) then inc(k);{找出a[k]与a[k+1]中较大值}
if a[0]<a[k] then begin a[i]:=a[k];i:=k;k:=2*i; end
else k:=m+1;
end;
a[i]:=a[0]; {将根放在合适的位置}
end;
procedure heapsort;
var
j:integer;
begin
for j:=n div 2 downto 1 do sift(j,n);
for j:=n downto 2 do begin
swap(a[1],a[j]);
sift(1,j-1);
end;
end;
第5题:
折半查找
function binsearch(k:keytype):integer;
var low,hig,mid:integer;
begin
low:=1;hig:=n;
mid:=(low+hig) div 2;
while (a[mid].key<>k) and (low<=hig) do begin
if a[mid].key>k then hig:=mid-1
else low:=mid+1;
mid:=(low+hig) div 2;
end;
if low>hig then mid:=0;
binsearch:=mid;
end;
第6题:
有如下程序:Private Sub Command1_Click() Dim k As Integer,m As Integer Dim op As Integer k=4:m=1 op:PPC(k,m):Print op; op:PPC(k,m):Print opEnd SubPrivate Function,PPC(a As Integer,b As Integer) Static m As Integer,i As Integer m=0:i=2 i=i+m+1 m=i+a+b PPC=mEnd Function程序运行后,输出的结果为
A.4 6
B.6 6
C.8 8
D.10 12