Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1879 Accepted Submission(s): 1133
int cal(int x) { int cnt = 0; while(x){ ++cnt; x &= (x-1); } return cnt; }
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 int T; 6 int m,n; 7 int A[105],b; 8 9 int cal(int x) 10 { 11 int cnt = 0; 12 while(x){ 13 ++cnt; 14 x &= (x-1); 15 } 16 return cnt; 17 } 18 19 int main() 20 { 21 scanf("%d",&T); 22 while(T--){ 23 scanf("%d%d",&m,&n); 24 for(int i = 0; i<m; ++i) 25 scanf("%d",&A[i]); 26 sort(A,A+m); 27 for(int i = 0; i<n; ++i){ 28 scanf("%d",&b); 29 int min_cnt = 0x7fffffff; 30 int ans; 31 for(int j = 0; j<m; ++j){ 32 int cnt = cal(b^A[j]); 33 if(cnt<min_cnt){ 34 min_cnt = cnt; 35 ans = A[j]; 36 } 37 } 38 printf("%d\n",ans); 39 } 40 } 41 return 0; 42 }
原文:http://www.cnblogs.com/inmoonlight/p/5180304.html