# 【題解】ZeroJudge c461: apcs 邏輯運算子 (Logic Operators)

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c461
【解題想法】直接檢查三個邏輯運算式是否成立。

• 依題意，「不等於零」的數字視為True，「等於零」的數字視為False。讀入測資時，一併轉換。接下來進行邏輯運算。
• 設立一個flag，當任一個邏輯運算式成立時，結果即為flag = True。
#include <iostream>
using namespace std;

int main(){
int a, b, c;
cin >> a >> b >> c;
if (a != 0) a = 1;
if (b != 0) b = 1;

bool flag = false;
if ((a && b) == c){
cout << "AND\n";
flag = true;
}

if ((a || b) == c){
cout << "OR\n";
flag = true;
}

if ((a != b) == c){
cout << "XOR\n";
flag = true;
}

if (!flag) cout << "IMPOSSIBLE\n";
return 0;
}
#include <iostream>
using namespace std;

int main(){
int a, b, c;
cin >> a >> b >> c;
if (a != 0) a = 1;
if (b != 0) b = 1;

bool flag = false;
if ((a && b) == c){
cout << "AND\n";
flag = true;
}

if ((a || b) == c){
cout << "OR\n";
flag = true;
}

if ((a ^ b) == c){
cout << "XOR\n";
flag = true;
}

if (!flag) cout << "IMPOSSIBLE\n";
return 0;
}

Python code

while True:
try:
a, b, c = map(int, input().split())
if a != 0:
a = 1
if b != 0:
b = 1
if c == 0:
c = False
else:
c = True
flag = False
if (a and b) == c:
print('AND')
flag = True
if (a or b) == c:
print('OR')
flag = True
if (a != b) == c:
print('XOR')
flag = True
if not flag:
print('IMPOSSIBLE')
except:
break