#include<bits/stdc++.h>
using namespace std;
int n;
int getBits1(int n)//求取一个数的二进制形式中1的个数.
{
int res=0;
while(n)
{
if(n&1) res++;
n>>=1;
}
return res;
} int getBits2(int n)
{
int res=0;
while(n)
{
res++;
n=n&(n-1);//一次消去1个1哦
}
return res;
} int main()
{
while(cin>>n)
{
cout<<getBits1(n)<<" "<<getBits2(n)<<endl;
}
return 0;
}
// 1= 1 1
// 2= 10 1
// 3= 11 2
// 4=100 1

题目:

For 2 non-negative integers x and y, f(x, y) is defined as the number of different bits in the binary format of x and y. For example, f(2, 3)=1,f(0, 3)=2, f(5, 10)=4. Now given 2 sets of non-negative integers A and B, for each integer b in B, you should find an integer a in A such that f(a, b) is minimized. If there are more than one such integer in set A, choose the smallest one.

输入:

The first line of the input is an integer T (0 < T ≤ 100), indicating the number of test cases. The first line of each test case contains 2 positive integers m and n (0 < m, n ≤ 100), indicating the numbers of integers of the 2 sets A and B, respectively. Then follow (m + n) lines, each of which contains a non-negative integers no larger than 1000000. The first m lines are the integers in set A and the other n lines are the integers in set B.

输出:

For each test case you should output n lines, each of which contains the result for each query in a single line.

样例输入:

2
2 5
1
2
1
2
3
4
5
5 2
1000000
9999
1423
3421
0
13245
353
样例输出:
1
2
1
1
1
9999
0
#include<bits/stdc++.h>
using namespace std;
int f(int n)
{
int res=0;
while(n)
{
res++;
n=n&(n-1);
}
return res;
} int main()
{
int a[105];
int T,n,m,i,j,k,minn,b,t;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++) scanf("%d",&a[i]);
for(i=0;i<m;i++)
{
scanf("%d",&b);
minn=f(a[0]^b);
k=0;
for(j=1;j<n;j++)
{
t=f(a[j]^b);
if(minn>t || minn==t&&a[k]>a[j])
{
minn=t;
k=j;
}
}
printf("%d\n",a[k]);
}
}
return 0;
}

  

 

Binary Number(位运算)的更多相关文章

  1. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. uestc 1709 Binary Operations 位运算的灵活运用

    Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674   Description   B ...

  3. 136.137.260. Single Number && 位运算

    136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...

  4. Same binary weight (位运算)

    题目描述 The binary weight of a positive  integer is the number of 1's in its binary representation.for ...

  5. Leetcode 268 Missing Number 位运算

    题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...

  6. 136. Single Number(位运算)

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  7. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  8. 位运算 UEST 84 Binary Operations

    题目传送门 题意:所有连续的子序列的三种位运算计算后的值的和的期望分别是多少 分析:因为所有连续子序列的组数有n * (n + 1) / 2种,所以要将他们分类降低复杂度,以ai为结尾的分成一组,至于 ...

  9. HDU 3006 The Number of set(位运算 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...

  10. LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor

    1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...

随机推荐

  1. 监控redis cluster 主从实例是否切换,切换前后对应关系

    需求:编写脚本实现对redis cluster 主从是否发生主从切换进行监控,若发生切换,输出切换前后主从对应关系. 初始化对比文件,仅执行一次,后续不需要在执行,除非该文件不存在. cmd=&quo ...

  2. 数据库 left()、length()函数

    数据库 left().length()函数 1.Mysql的length()函数: length()函数主要用于计算字符串的长度,用法也很简单:length(要计算的字符串) 就可以计算出字符串的长度 ...

  3. JSP数据交互2

    application对象 application对象负责提供应用程序在服务器中运行时的一些全局信息,常用的方法有getMimeType和getRealPath等. 运用实例:网页访问计数器. < ...

  4. JS格式时间

    Date.prototype.format = function(format) { var o = { "M+": this.getMonth() + 1, //month &q ...

  5. Jquery实现挂号平台首页源码2

    第二个版本:点击预约挂号可跳转到排班表,获取之后7个星期的排班 先放图 首先是index.html <!DOCTYPE html> <html lang="en" ...

  6. JMeter-显示调试日志log

    JMeter-调试日志记录 参考文档:https://jmeter.apache.org/usermanual/hints_and_tips.html 大多数测试元素包括调试日志记录. 如果从GUI运 ...

  7. 关于 setw() 函数(C++)

    // about setw() #include <iostream> #include <iomanip> #include <cstring> using na ...

  8. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分)

    People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...

  9. 折半枚举+Hash(HDU1496升级版)

    题目链接:N - 方程的解 给定一个四元二次方程: Ax1^2+Bx2^2+Cx3^2+Dx4^2=0 试求−1000≤x1,x2,x3,x4≤1000非零整数解的个数. −10000≤A,B,C,D ...

  10. vue踩坑:vue+ element ui 表单验证有值但验证失败。

    一.如图:有值但是验证失败 二. <el-form :model="form" :rules="rules"> <el-form-item l ...