pascal编程用合并的方法把两个有序数列A,B合并为一个新的有序数列C,不得先合并在排序,求源程序和解释。
答案:1 悬赏:60 手机版
解决时间 2021-03-19 01:00
- 提问者网友:雨不眠的下
- 2021-03-18 05:15
pascal编程用合并的方法把两个有序数列A,B合并为一个新的有序数列C,不得先合并在排序,求源程序和解释。
最佳答案
- 五星知识达人网友:第四晚心情
- 2021-03-18 05:29
t:=0; h1;=1; h2:=1;
repeat
inc(t);
if (a[h1] 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);
if h1<=n1 then
begin
for i:=h1 to n1 do
begin
inc(t);
c[t]:=a[i];
end;
end;
if h2<=n2 then
begin
for i:=h2 to n2 do
begin
inc(t);
c[t]:=b[i];
end;
end;追问您可不可以把全程还有解释和输出输入写一下啊。谢谢!
追答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;
repeat
inc(t);
if (a[h1] 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);
if h1<=n1 then
begin
for i:=h1 to n1 do
begin
inc(t);
c[t]:=a[i];
end;
end;
if h2<=n2 then
begin
for i:=h2 to n2 do
begin
inc(t);
c[t]:=b[i];
end;
end;
for i:=1 to t do write(c[i],' ');
end.追问那您可以告诉我一下每一步的大概意思吗?谢谢了!~
还有它说取小的数比较,是怎么取 啊?追答看两个队列的头,谁小就把谁放到答案数组里
repeat
inc(t);
if (a[h1] 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);
if h1<=n1 then
begin
for i:=h1 to n1 do
begin
inc(t);
c[t]:=a[i];
end;
end;
if h2<=n2 then
begin
for i:=h2 to n2 do
begin
inc(t);
c[t]:=b[i];
end;
end;追问您可不可以把全程还有解释和输出输入写一下啊。谢谢!
追答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;
repeat
inc(t);
if (a[h1] 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);
if h1<=n1 then
begin
for i:=h1 to n1 do
begin
inc(t);
c[t]:=a[i];
end;
end;
if h2<=n2 then
begin
for i:=h2 to n2 do
begin
inc(t);
c[t]:=b[i];
end;
end;
for i:=1 to t do write(c[i],' ');
end.追问那您可以告诉我一下每一步的大概意思吗?谢谢了!~
还有它说取小的数比较,是怎么取 啊?追答看两个队列的头,谁小就把谁放到答案数组里
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯