我怎样才能最好地检查两个无序对是否匹配?

How can I best check if two unordered pairs match?

我有两对 (A,B) 和 (C,D)。我想检查它们是否包含相同的元素。顺序未知。我可以使用以下方法完成此操作:

(A == C && B == D) || (A == D && B == C)

但这似乎是非常统一和重复的代码,好像有一种方法可以压缩它。我无法弄清楚如何。是否可以压缩该逻辑?

(A == C && B == D) || (A == D && B == C)

真相Table

+------+------+------+------+----------+
|   A  |  B   |  C  |   D  |   O/P  |
+------+------+------+------+----------+
|   0  |   0   |   0  |   0  |    1     |
+------+------+------+------+----------+
|   0   |   0  |   0  |  1   |    0     |
+------+------+------+------+----------+
|   0   |   0  |  1   |  0   |    0     |
+------+------+------+------+----------+
|   0   |   0  |  1   |   1  |    0     |
+------+------+------+------+----------+
|   0   |   1  |   0  |   0  |   0      |
+------+------+------+------+----------+
|    0  |   1  |   0  |  1   |    1     |
+------+------+------+------+----------+
|    0  |   1  |  1   |  0   |   1      |
+------+------+------+------+----------+
|    0  |  1   |  1   |   1  |   0      |
+------+------+------+------+----------+
|   1   |   0  |   0  |   0  |   0      |
+------+------+------+------+----------+
|   1   |   0  |   0  |  1   |     1    |
+------+------+------+------+----------+
|   1   |   0  |   1  |  0   |    1     |
+------+------+------+------+----------+
|   1   |   0  |  1   |  1   |    0     |
 +------+------+------+------+----------+
|    1  |   1  |  0   |   0  |     0    |
+------+------+------+------+----------+
|   1   |   1  |   0  |  1   |     0    |
+------+------+------+------+----------+
|    1  |  1   |   1  |  0   |     0    |
+------+------+------+------+----------+
|    1  |   1  |  1   |  1   |   0      |
+------+------+------+------+----------+
  • 如果 A、B、C、D 是布尔逻辑则 (A == C && B == D) || (A == D && B == C) 是 K-map 缩减技术和 Quine-McCluskey 最小化技术的最简单形式。

  • 如果 A、B、C、D 是数字 (ie int or float) 那么你可以简单地理解为 A+B==C+D .

if(A+B==C+D) { //true statment }

  • 如果 A、B、C、D 是字符串,则布尔逻辑不适用。它可能会被简化,但需要考虑其他信息来解决表达式。

谢谢希望你满意