#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. 深度优先搜索DFS---全球变暖

    内心OS:这道题是去年准备HD复试时,我用来练习DFS的.现在再做这道题,感触颇深,唉,时光蹉跎,物是人非啊~~ 题目: 你有一张某海域NxN像素的照片,”.”表示海洋.”#”表示陆地,如下所示: … ...

  2. pyqt5-进度条控制

    1.基于自定义类的方式 继承自QProgressBar类,然后重写timerEvent方法,当该组件设置定时器的时候,会自己处理定时的处理方法,完成相应的功能 from PyQt5.Qt import ...

  3. python全栈学习 day04

    列表基本操作: #!/usr/bin/env python # -*- coding:utf-8 -*- ''' li = ['alex', [1, 2, 3], 'wusir', 'godness' ...

  4. [CTSC2008]网络管理 [树剖+整体二分]

    这题的复杂度可以到达惊人的\(\log^4\)据说还能跑过去(差点没吓死我 直接二分+树剖树套树(\(n \log^4 n\)) 一个\(\log\)也不少的4\(\log\) 但是我有个\(\log ...

  5. gulp常用插件之gulp-rev使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-rev这是一款为静态文件随机添加一串hash值, 解决cdn缓存问题, a.css --> a-d2f3f35d3.css.根据静 ...

  6. 解决NahimicSvc32.exe与bilibili直播姬的音频不兼容的问题

    某次测试哔哩哔哩直播姬的时候发现系统声音采集异常的错误 NahimicSvc32.exe是NahimicService下的程序,奇怪的是我的本本所有硬件没有一个微星有关系,怎么就装上了微星的服务程序? ...

  7. PIE-SDK For C++矢量数据空间索引的创建

    1.功能简介 空间索引的使用便于数据的查询:所以在创建矢量数据的时候创建空间索引,下面对矢量数据如何创建空间索引进行功能介绍. 2.功能实现说明 2.1 实现思路及原理说明 第一步 创建矢量要素数据集 ...

  8. 获取url参数(jq 扩展包)

    (function($){ $.extend({ urlGet:function(url) { var getUrl = url ? url.split("?") : window ...

  9. 0005 uwsgi配置

    在配置文件目录Configurations下创建一个名为uwsgi.ini的文件,用于uwsgi服务配置. uwsgi在服务器上使用,接收nginx的转发请求. 内容如下: # 配置文件:这一行必须有 ...

  10. 熵权法(the Entropy Weight Method)以及MATLAB实现

    按照信息论基本原理的解释,信息是系统有序程度的一个度量,熵是系统无序程度的一个度量:如果指标的信息熵越小,该指标提供的信息量越小,在综合评价中所起作用理当越小,权重就应该越低.因此,可利用信息熵这个工 ...