三个boolean值至少两个为ture,则判为true

题目描述:

三个boolean值至少两个为ture,则判为true。

解题思路:

这题是朋友问我的一个问题,网上查了下,发现是一道面试题。该题的解决方案有很多,我使用的方法如下表格所示。
首先求出 a 异或 b,然后发现,当 a ⊕ \oplus b = 0 的时候,结果为 a 的值;当 a ⊕ \oplus b = 1 的时候,结果为 c 的值。

a ⊕ \oplus babcresult
0 1 ‾ \color{red}{\underline{1}} 1111
0 1 ‾ \color{red}{\underline{1}} 1101
110 1 ‾ \color{red}{\underline{1}} 11
101 1 ‾ \color{red}{\underline{1}} 11
0 0 ‾ \color{red}{\underline{0}} 0010
101 0 ‾ \color{red}{\underline{0}} 00
110 0 ‾ \color{red}{\underline{0}} 00
0 0 ‾ \color{red}{\underline{0}} 0000
public boolean atLeastTwo(boolean a, boolean b, boolean c) {
    return a ^ b ? c : a;
}

另外还有多种解法。 网上搜到的: https://blog.csdn.net/m0_38098232/article/details/73457120

解法1:

return a ? (b || c) : (b && c);

解法2:

return (a==b) ? a : c;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值