我有两对(A,B)和(C,D)。我想检查它们是否包含相同的元素。订单未知。我可以使用以下方式完成此任务:
(A == C && B == D) || (A == D && B == C)
但这似乎是非常统一和重复的代码,好像有一种方法可以压缩它。我无法弄清楚如何。是否有可能压缩这种逻辑?
答案 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) { //真实的陈述 }
谢天谢地,希望你满意