题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3711
题意:两个数集合,找二进制下位数不同最少的数,如果一样,找集合数最小的。
暴力枚举
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 110; 5 int n, m; 6 int a[maxn], b[maxn]; 7 8 int ok(int x, int y) { 9 int xx = x, yy = y; 10 11 int cnt = 0; 12 if(x > y) swap(x, y); 13 while(x) { 14 if((x&1)!=(y&1)) cnt++; 15 x >>= 1; y >>= 1; 16 } 17 while(y) { 18 if(y&1) cnt++; 19 y >>= 1; 20 } 21 return cnt; 22 } 23 24 int main() { 25 //freopen("in", "r", stdin); 26 int T; 27 scanf("%d", &T); 28 while(T--) { 29 scanf("%d %d", &n, &m); 30 for(int i = 1; i <= n; i++) scanf("%d", &a[i]); 31 for(int i = 1; i <= m; i++) scanf("%d", &b[i]); 32 for(int i = 1; i <= m; i++) { 33 int ret = 9000000, k; 34 for(int j = 1; j <= n; j++) { 35 int tmp = ok(b[i], a[j]); 36 if(ret > tmp) { 37 ret = tmp; 38 k = j; 39 } 40 else if(ret == tmp) { 41 if(a[k] > a[j]) k = j; 42 } 43 } 44 cout << a[k] << endl; 45 } 46 } 47 return 0; 48 }