#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. 根据ip列表模拟输出redis cluster的主从对应关系

    需求:给点一批ip列表,一个数组或者一个文件,每行一个ip,模拟输出redis cluster的组从关系,前者是master_ip:master_port -> slave_ip:slave_p ...

  2. (LNMP) Nginx_PHP_MariaDB

    L用的是Centos7.5以上,主要是NMP三组件的安装记录. 通常会先安装一下依赖: yum install -y pcre-devel zlib-devel openssl-devel 使用yum ...

  3. 浅谈python的第三方库——pandas(二)

    pandas使用小贴士 1 通过Series创建DataFrame 在pandas系列的第一篇博文中曾提到,Series可视为DataFrame的一种特例,即只有一列数据.既然如此,是否可以并列多个S ...

  4. 20200228 尚硅谷-NIO

    尚硅谷-NIO Java NIO简介 Java NIO(New IO.Non Blocking IO)是从Java1.4版本开始引入的新的 IO API,可以替代标准的 Java IO API. NI ...

  5. 安装JumpServer到CentOS(YUM)

    运行环境 系统版本:CentOS Linux release 7.6.1810 (Core) 软件版本:JumpServer-1.4.8 硬件要求:最低2核4GB 官方文档:https://docs. ...

  6. 吴裕雄--天生自然HADOOP操作实验学习笔记:mapreduce和yarn命令

    实验目的 了解集群运行的原理 学习mapred和yarn脚本原理 学习使用Hadoop命令提交mapreduce程序 学习对mapred.yarn脚本进行基本操作 实验原理 1.hadoop的shel ...

  7. 【Debian学徒记事】Debian快速呼出Terminal终端

    Debian快速呼出Terminal终端 书接上回,Debian已经安装完毕 失踪的Ctrl+Alt+T 安装完毕启动,我发现了剑很诡异的事,Ctrl+Alt+T居然失灵了 (在多次测试后发现,Deb ...

  8. sql server 发送邮件

    -- BI EMAIL declare @CC varchar(10),@MAIL varchar(500), @str varchar(800),@year varchar(4),@month va ...

  9. MySQL数据库优化(一)

    1.1.1. 慢查询日志当查询超过一定的时间没有返回结果的时候,才会记录到慢查询日志中.默认不开启.采样的时候手工开启.可以帮助我们找出执行慢的 SQL 语句查看慢 SQL 日志是否启用(on 表示启 ...

  10. Tiptop ERP 采购运费一键分摊

    项目背景: 公司的采购运费在逐年上升,之前财务都是做在管理费用中,金额大了后已经严重造成成本失真,所以财务要求it部能帮助分摊运费   1.纸质单据 2.系统入库单apmt720 3.系统请款单apm ...