【问题标题】:Simplify logic statement简化逻辑语句
【发布时间】:2015-12-01 05:56:55
【问题描述】:

我有这个:

if(!A or (A and B))   //pseudocode

我想否定那个 if 语句:

这应该可行:

if(!(!A or (A and B)))  //pseudocode

但我相信有一种方法可以简化它,而现在它正在逃避我。

【问题讨论】:

标签: if-statement boolean-operations


【解决方案1】:

欢迎来到布尔代数和简单分布的德摩根世界:

if(!(!A or (A and B)) 
=> if(!(!A) and !(A and B))
=> if(A and (!A or !B))
=> if((A and !A) or (A and !B))
=> if(A and !B) 

【讨论】:

  • 感谢这似乎与 wolfram alpha 的输出相吻合
【解决方案2】:

如果你把它分解成一个真值表......

A B  !A  (A and B)  A! or (A and B)
0 0  1   0          1
0 1  1   0          1
1 0  0   0          0
1 1  0   1          1

您可以在除A and !B 之外的所有情况下看到结果为真,而无需知道/记住任何布尔代数规则。 “除了”只是“不是”所以...!(A and !B)...

但是,如果编写 !A or (A and B) 更适合您尝试编写代码的实际问题,请不要这样做,除非它对性能非常关键......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多