【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e797
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int a[11][11];
int AND[11], OR[11], XOR[11];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, T;
cin >> N >> T;
for (int i=0; i<N; i++){
for (int j=0; j<T; j++){
cin >> a[i][j];
}
}
for (int i=0; i<T; i++){
int ones = 0;
for (int j=0; j<N; j++){
ones += (a[j][i] == 1);
}
if (ones == N) AND[i] = 1;
if (ones > 0) OR[i] = 1;
if (ones % 2) XOR[i] = 1;
}
cout << "AND:";
for (int i=0; i<T; i++) {
cout << ' ' << AND[i];
}
cout << '\n';
cout << " OR:";
for (int i=0; i<T; i++) {
cout << ' ' << OR[i];
}
cout << '\n';
cout << "XOR:";
for (int i=0; i<T; i++) {
cout << ' ' << XOR[i];
}
cout << '\n';
return 0;
}
Python code (credit: Amy Chou)
N, T = map(int, input().split())
a = []
for i in range(N):
a.append(list(map(int, input().split())))
AND = []
OR = []
XOR = []
for i in range(T):
ones = 0
for j in range(N):
ones += (a[j][i] == 1)
if ones == N:
AND.append(1)
else:
AND.append(0)
if ones >= 1:
OR.append(1)
else:
OR.append(0)
if ones % 2:
XOR.append(1)
else:
XOR.append(0)
print("AND:", end='')
for i in range(T):
print(" ", AND[i], sep='', end='')
print()
print(" OR:", end='')
for i in range(T):
print(" ", OR[i], sep='', end='')
print()
print("XOR:", end='')
for i in range(T):
print(" ", XOR[i], sep='', end='')
print()