program Project1;
var
a,b:set of byte;
c:set of byte;
i:integer;
begin
a:=[3];
b:=[2];
c:=(a+b)*(a-b);
FOR i:= 0 TO 5 DO
IF i IN c THEN write(i:2);
readln;
end.
有人可以向我解释一下这段代码中发生了什么。我知道c = 3但不明白a和b的值是什么?试过writeln(a,b);
但是给了我一个错误......
答案 0 :(得分:1)
好的,我会解释你可能通过调试找到的东西,或者只是简单地阅读Pascal上的教科书:
该行:
c := (a+b)*(a-b);
执行以下操作:
a + b is the union of the two sets, i.e. all elements that are
in a or in b or in both, so here, that is [2, 3];
a - b is the difference of the two sets, i.e. it is a minus the
elements of a that happen to be in b too. In this case, no
common elements to be removed, so the result is the same as a,
i.e. [3]
x * y is the intersection of the two sets, i.e. elements that are in
x as well as in y (i.e. a set with the elements both have in common).
说,x := a + b; y := a - b;
,然后可以将其视为:
x := a + b; // [3] + [2] --> [2, 3]
y := a - b; // [3] - [2] --> [3]
c := x * y; // [2, 3] * [3] --> [3]