代码之家  ›  专栏  ›  技术社区  ›  Zoran Mladenovski

字节集,a和b的值是多少

  •  -4
  • Zoran Mladenovski  · 技术社区  · 6 年前
    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); 但给了我一个错误。。。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Rudy Velthuis    6 年前

    好的,我将解释您可能通过调试或简单地阅读有关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]