发布网友 发布时间:2024-10-23 22:45
共1个回答
热心网友 时间:2024-11-01 18:58
var
a,b,c:array[1..100] of longint;
h1,h2,n1,n2,t,i:longint;
begin
read(n1,n2);
for i:=1 to n1 do read(a[i]);
for i:=1 to n2 do read(b[i]);
t:=0; h1:=1; h2:=1; {h1,h2:分别指向a,b待处理的元素下标}
repeat
inc(t); {t:指向合并后c的元素下标}}
if (a[h1]<b[h2]) then {选择小的元素放入c中}
begin
c[t]:=a[h1]; {复制元素}
inc(h1); {跳过已复制的元素}
end
else begin
c[t]:=b[h2];
inc(h2);
end;
until (h1=n1+1)or(h2=n2+1); {直到a,b中至少有一个为空}
if h1<=n1 then {如a数列未处理完}
begin
for i:=h1 to n1 do {将a数列中剩余元素全部复制到c中}
begin
inc(t);
c[t]:=a[i];
end;
end;
if h2<=n2 then {如b数列未处理完}
begin
for i:=h2 to n2 do {将b数列中剩余元素全部复制到c中}
begin
inc(t);
c[t]:=b[i];
end;
end;
for i:=1 to t do write(c[i],' ');end. {输出完成后c数列}