#23809: 為甚麼這個不過?高手指導一下


lee1030075@gmail.com (hi)

學校 : 不指定學校
編號 : 100004
來源 : [123.193.11.80]
最後登入時間 :
2021-06-12 14:12:40
c461. apcs 邏輯運算子 (Logic Operators) -- apcs | From: [123.193.11.80] | 發表日期 : 2020-12-20 10:42

#include <bits/stdc++.h>
#include <cstring>
using namespace std;


int main() {
int a,b,c;
cin>>a>>b>>c;
bool and1 = a&b;
bool or1 = a|b;
bool xor1 = a^b;
if(and1==1 && c == 1) {
cout<<"AND"<<endl;
}
else if(and1 == 0 && c == 0){
cout<<"AND"<<endl;
}
if(or1==1 && c == 1) {
cout<<"OR"<<endl;
}
else if(or1 == 0 && c == 0){
cout<<"OR"<<endl;
}
if(xor1==1 && c==1) {
cout<<"XOR"<<endl;
}
else if(xor1 == 0 && c == 0){
cout<<"XOR"<<endl;
}
if(((and1==1 && c == 0)&&(and1 == 0 && c == 1))&&
((or1==1 && c == 0)&&(or1 == 0 && c == 1))&&
((xor1==1 && c==0)&&(xor1 == 0 && c == 1))){
cout<<"IMPOSSIBLE"<<endl;
}
}

 
 
#23828: Re:為甚麼這個不過?高手指導一下


mirkat.ee06@g2.nctu.edu.tw (炭烤海苔)

學校 : 不指定學校
編號 : 74539
來源 : [138.246.3.111]
最後登入時間 :
2023-11-22 18:43:09
c461. apcs 邏輯運算子 (Logic Operators) -- apcs | From: [140.113.95.216] | 發表日期 : 2020-12-23 19:11


 


因為你用的是bitwise operator,但仔細看題目的表格,當 a為0 時 a=0 ,當 a不為0 時 a=1,而c只有0和1

 

BTW

我覺得你的程式碼的 if 判斷式都挺多此一舉的

像是

if(and1==1 && c == 1)

你其實直接寫

if(and1==c )

就好了

 

 
ZeroJudge Forum