Check if at least two out of three booleans are true

后端 未结 30 1864
自闭症患者
自闭症患者 2020-11-28 16:47

An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true.

My solution follow

30条回答
  •  借酒劲吻你
    2020-11-28 17:44

    This kind of questions can be solved with a Karnaugh Map:

          | C | !C
    ------|---|----
     A  B | 1 | 1 
     A !B | 1 | 0
    !A !B | 0 | 0
    !A  B | 1 | 0
    

    from which you infer that you need a group for first row and two groups for first column, obtaining the optimal solution of polygenelubricants:

    (C && (A || B)) || (A && B)  <---- first row
           ^
           |
       first column without third case
    

提交回复
热议问题