[백준 13234] George Boole (C#, C++) - soo:bak
작성일 :
문제 링크
설명
불 연산을 수행하는 상황에서, 두 개의 불리언 값(true 또는 false)과 하나의 연산자(AND 또는 OR)가 주어질 때, 연산 결과를 구하는 문제입니다.
AND 연산은 두 값이 모두 true일 때만 true를 반환하고, OR 연산은 두 값 중 하나라도 true면 true를 반환합니다. 결과는 true 또는 false로 출력합니다.
접근법
입력으로 주어진 문자열을 파싱하여 불리언 값과 연산자를 구분합니다.
왼쪽 값과 오른쪽 값을 불리언으로 변환한 후, 연산자가 AND인지 OR인지에 따라 적절한 논리 연산을 수행합니다.
최종 결과를 문자열 형태(true 또는 false)로 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
namespace Solution {
class Program {
static void Main(string[] args) {
var input = Console.ReadLine()!.Split();
var leftValue = input[0] == "true";
var operation = input[1];
var rightValue = input[2] == "true";
var result = operation == "AND" ? (leftValue && rightValue) : (leftValue || rightValue);
Console.WriteLine(result ? "true" : "false");
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string leftStr, operation, rightStr;
cin >> leftStr >> operation >> rightStr;
bool leftValue = (leftStr == "true");
bool rightValue = (rightStr == "true");
bool result = (operation == "AND") ? (leftValue && rightValue) : (leftValue || rightValue);
cout << (result ? "true" : "false") << "\n";
return 0;
}