如何才能最好地检查两个无序对是否匹配?

时间:2016-03-23 21:55:39

标签: optimization logic boolean-logic

我有两对(A,B)和(C,D)。我想检查它们是否包含相同的元素。订单未知。我可以使用以下方式完成此任务:

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

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

1 个答案:

答案 0 :(得分:1)

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

真相表

+------+------+------+------+----------+
|   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

  

如果(A + B == C + d)       {             //真实的陈述        }

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

谢天谢地,希望你满意