Skip to content

Commit 4070e8b

Browse files
committed
Create Check-if-at-least-two-out-of-three-booleans-are-true.md
1 parent c2ebd5e commit 4070e8b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
给3个布尔变量,当其中有2个或者2个以上为true菜返回true
2+
===
3+
问题
4+
给3个boolean变量,a,b,c,当其中有2个或2个以上为true时才返回true?
5+
最笨的方法:
6+
```java
7+
boolean atLeastTwo(boolean a, boolean b, boolean c)
8+
{
9+
if ((a && b) || (b && c) || (a && c))
10+
{
11+
return true;
12+
}
13+
else
14+
{
15+
return false;
16+
}
17+
}
18+
```
19+
* 优雅解法1
20+
```java
21+
return a ? (b || c) : (b && c);
22+
```
23+
24+
* 优雅解法2
25+
```java
26+
return (a==b) ? a : c;
27+
```
28+
29+
* 优雅解法3
30+
```java
31+
return a ^ b ? c : a
32+
```
33+
34+
* 优雅解法4
35+
```java
36+
return a ? (b || c) : (b && c);
37+
```
38+
39+
stackoverflow链接: http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true

0 commit comments

Comments
 (0)