一组字节,a 和 b 的值是多少
Set of bytes, what are the values of a and b
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);
但给我一个错误 ...
好的,我将解释您可能通过调试或阅读 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]
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);
但给我一个错误 ...
好的,我将解释您可能通过调试或阅读 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]