[HDOJ3711]Binary Number(枚举)

时间:2023-12-23 15:49:20

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3711

题意:两个数集合,找二进制下位数不同最少的数,如果一样,找集合数最小的。

暴力枚举

 #include <bits/stdc++.h>
using namespace std; const int maxn = ;
int n, m;
int a[maxn], b[maxn]; int ok(int x, int y) {
int xx = x, yy = y; int cnt = ;
if(x > y) swap(x, y);
while(x) {
if((x&)!=(y&)) cnt++;
x >>= ; y >>= ;
}
while(y) {
if(y&) cnt++;
y >>= ;
}
return cnt;
} int main() {
//freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i <= m; i++) scanf("%d", &b[i]);
for(int i = ; i <= m; i++) {
int ret = , k;
for(int j = ; j <= n; j++) {
int tmp = ok(b[i], a[j]);
if(ret > tmp) {
ret = tmp;
k = j;
}
else if(ret == tmp) {
if(a[k] > a[j]) k = j;
}
}
cout << a[k] << endl;
}
}
return ;
}