你已经有这个想法了。 为了表达“n 个中的 k 个成立”,您需要枚举 k 个成立的所有情况。 因此,如果我们有变量 ABCDE,并且您想要 5 中的 3,则需要
(A & B & C & ~D & ~E) |
(A & B & ~C & D & ~E) |
(A & B & ~C & ~D & E) | ...
(~A & ~B & C & D & E)
where & 是“和”,| 是“或”,〜是“非”。
You've got the idea there. To express "k of n holds" you're going to need to enumerate all cases for which k holds. So, if we have variables A B C D E, and you want 3 of 5, you'll need
(A & B & C & ~D & ~E) |
(A & B & ~C & D & ~E) |
(A & B & ~C & ~D & E) | ...
(~A & ~B & C & D & E)
I would count them. However if you must produce a predicate using boolean operations only then you could treat the inputs as bits into a system of adders.
Basic Half-Adder
A, B : 1st 2nd bits
O, C : unit output and carry to next unit
O := A xor B;
C := A and B;
Then you can group up the six variables into 3 pairs, then figure out how to use those outputs to get closer to the answer and solve the rest yourself.
Another option would be to use a circuit to find pop count (sideways add) and check to see if the only bit matches for 2. Famous example in assembly not logic gates is [Hakmem 169][http://home.pipeline.com/~hbaker1/hakmem/hacks.html#item169 (popcount)].
对于变量集 {A..F},Pax 和 Charlie Martin 的回答涵盖了“恰好两个”情况(两个为真,其余为假),而您问题中的表达式似乎处理“在至少两个”情况:
(A && B) || (A && C) || ... || (D && E) || (D && F) || (E && F)
是一个表达式,当 A 和 B 为真且其余变量为任意值(真或假)时,该表达式为真。
如果您要求的是一个类似于集合论的表达式来描述上面的情况,您可能会这样表达:
#{x | x <- {A, B, C, D, E, F} | x} = 2
其中符号的工作方式如下:
#{...}
代表封闭的集合和集合本身:
{x | x <- {A, B, C, D, E, F} | x}
读取“x 的集合,其中 x 是 A 到 F 之一code>,并且 x 为 true”。 换句话说,给定变量集 A 到 F,由具有真值的变量组成的子集恰好有两个元素。 (使用 <= 而不是“=”来表达您问题的其他解释。)
It makes a difference whether you mean "exactly two must be true" versus "at least two must be true".
For the set of variables {A..F}, the responses by Pax and Charlie Martin covered the "exactly two" situation (two are true and the rest are false), while the expression in your question seemed to deal with the "at least two" case:
(A && B) || (A && C) || ... || (D && E) || (D && F) || (E && F)
is an expression that is true when, for example, A and B are true and the remaining variables are anything (true or false).
If what you're asking for is a set-theory-like expression to describe the situation(s) above, you might express it something like this:
#{x | x <- {A, B, C, D, E, F} | x} = 2
where the notation works this way:
#{...}
represents the size of the enclosed set, and the set itself:
{x | x <- {A, B, C, D, E, F} | x}
reads "the set of x, where x is one of A through F, and x is true". In other words given the set of variables A through F, the subset made up of the variables with true values has exactly two elements. (Use <= instead of '=' to express the other interpretation of your question.)
发布评论
评论(7)
在布尔逻辑中(v 是 OR,谓词后面的 ' 是 NOT):
对于排列,您需要编写大量子表达式。
当然,如果这是一个编程问题,你可以将布尔值转换为0或1,然后将它们全部相加并确保结果为2。
In boolean logic (v is OR, ' following the predicate is NOT):
With permutations, there's a great many subexpressions you need to write.
Of course, if this is a programming question, you could just convert the booleans to 0 or 1, add them all up and ensure the result is 2.
假设 C# 或其他一些语言,其中 bool != int:
Assuming C# or some other language where bool != int:
Python:
PHP:
Python:
PHP:
你已经有这个想法了。 为了表达“n 个中的 k 个成立”,您需要枚举 k 个成立的所有情况。 因此,如果我们有变量 ABCDE,并且您想要 5 中的 3,则需要
where & 是“和”,| 是“或”,〜是“非”。
You've got the idea there. To express "k of n holds" you're going to need to enumerate all cases for which k holds. So, if we have variables A B C D E, and you want 3 of 5, you'll need
where & is "and", | is "or" and ~ is "not".
假设“A或更多”,
做得更好一点
代码来
您可以通过构建树或更好的
Assuming "A or more"
you can do a bit better by building a tree
or as code
better yet
我会数数。
但是,如果您必须仅使用布尔运算生成谓词,那么您可以将输入视为加法器系统中的位。
您可以在 [Wikipedia][http://en.wikipedia.org/wiki/ 找到更多示例和链接Half_adder (Half-Adder)]
然后你可以将六个变量分成 3 对,然后弄清楚如何使用这些输出来更接近答案并自己解决其余的问题。
另一种选择是使用电路来查找弹出计数(横向相加)并检查唯一的位是否与 2 匹配。
汇编而非逻辑门中的著名示例是 [Hakmem 169][http:// home.pipeline.com/~hbaker1/hakmem/hacks.html#item169(popcount)]。
I would count them.
However if you must produce a predicate using boolean operations only then you could treat the inputs as bits into a system of adders.
You can find more examples and links at [Wikipedia][http://en.wikipedia.org/wiki/Half_adder (Half-Adder)]
Then you can group up the six variables into 3 pairs, then figure out how to use those outputs to get closer to the answer and solve the rest yourself.
Another option would be to use a circuit to find pop count (sideways add) and check to see if the only bit matches for 2.
Famous example in assembly not logic gates is [Hakmem 169][http://home.pipeline.com/~hbaker1/hakmem/hacks.html#item169 (popcount)].
无论您的意思是“恰好两个必须为真”还是“至少两个必须为真”,这都是有区别的。
对于变量集 {A..F},Pax 和 Charlie Martin 的回答涵盖了“恰好两个”情况(两个为真,其余为假),而您问题中的表达式似乎处理“在至少两个”情况:
是一个表达式,当 A 和 B 为真且其余变量为任意值(真或假)时,该表达式为真。
如果您要求的是一个类似于集合论的表达式来描述上面的情况,您可能会这样表达:
其中符号的工作方式如下:
代表封闭的集合和集合本身:
读取“
x
的集合,其中x
是A
到F
之一code>,并且x
为 true”。 换句话说,给定变量集A
到F
,由具有真值的变量组成的子集恰好有两个元素。 (使用<=
而不是“=”来表达您问题的其他解释。)It makes a difference whether you mean "exactly two must be true" versus "at least two must be true".
For the set of variables {A..F}, the responses by Pax and Charlie Martin covered the "exactly two" situation (two are true and the rest are false), while the expression in your question seemed to deal with the "at least two" case:
is an expression that is true when, for example, A and B are true and the remaining variables are anything (true or false).
If what you're asking for is a set-theory-like expression to describe the situation(s) above, you might express it something like this:
where the notation works this way:
represents the size of the enclosed set, and the set itself:
reads "the set of
x
, wherex
is one ofA
throughF
, andx
is true". In other words given the set of variablesA
throughF
, the subset made up of the variables with true values has exactly two elements. (Use<=
instead of '=' to express the other interpretation of your question.)