火炬之光2熔岩相位在哪:pascal语言程序:找并集

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/01 19:06:11
对于给定的几个整数集合,求这些集合的并集。
输入输出要求
输入文件assoc.in第一行为集合的个数n(1<n<10),以下n行,各行为一个集合的描述,第一数为该集合的元素个数m,以后跟着m个-32767到32767之间的整数。
输出文件assoc.out第一行为并集元素的个数k,以下k行各有一个数,即并集的各元素按从小到大的排列结果。
输入输出样例
assoc.in
3
3 1 2 3
2 -1 2 3
4 2 3 4 5
assoc.out
6
-1
1
2
3
4
5
请用pascal语言编译,或者告诉我算法(用二叉树)!谢谢!

const
inf='assoc.in';
ouf='assoc.out';
type
ptr=^rec;
rec=record
l,r:ptr;
data:longint;
end;
var
bst:ptr;
n,m,i,j,p,k:longint;

procedure insert(p:longint;var bst:ptr);
begin
if bst=nil then begin
new(bst);
bst^.data:=p;
bst^.l:=nil;bst^.r:=nil;
inc(k);
end
else begin
if p<bst^.data then insert(p,bst^.l)
else if p>bst^.data then insert(p,bst^.r);
end;
end;

procedure search(bst:ptr);
begin
if bst<>nil then begin
search(bst^.l);
writeln(bst^.data);
search(bst^.r);
end;
end;

begin
assign(input,inf);
reset(input);
k:=0;
new(bst);bst:=nil;
readln(n);
for i:=1 to n do begin
read(m);
for j:=1 to m do begin
read(p);
insert(p,bst);
end;
readln;
end;
close(input);
assign(output,ouf);
rewrite(output);
writeln(k);
search(bst);
close(output);
end.

个人认为没必要用二叉树

可以用数组解决把,没必要用二叉树