Post

[Programmers] #181917 - 간단한 논리 연산 [Java][C++][Python]

네 boolean 값에 OR·AND를 섞은 논리식을 그대로 구현하는 워밍업 문제.

[Programmers] #181917 - 간단한 논리 연산 [Java][C++][Python]

문제 링크


1. 아이디어

$(x_1 \lor x_2) \land (x_3 \lor x_4)$ 식을 언어의 논리 연산자로 그대로 옮기면 되는 문제다. x1, x2 중 하나라도 참이면 앞쪽 조건이 참이 되고, x3, x4 중 하나라도 참이면 뒤쪽 조건이 참이 되며, 두 조건이 모두 참일 때만 전체 결과가 참이 된다.


2. 복잡도

접근시간공간
풀이$O(1)$$O(1)$

3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
class Solution {
    public boolean solution(boolean x1, boolean x2, boolean x3, boolean x4) {
        return (x1 || x2) && (x3 || x4);
    }
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

bool solution(bool x1, bool x2, bool x3, bool x4) {
    return (x1 || x2) && (x3 || x4);
}
1
2
def solution(x1, x2, x3, x4):
    return (x1 or x2) and (x3 or x4)

This post is licensed under CC BY 4.0 by the author.