Binary Number(位运算)

#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(位运算)的更多相关文章
- [HDU] 3711 Binary Number [位运算]
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- uestc 1709 Binary Operations 位运算的灵活运用
Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674 Description B ...
- 136.137.260. Single Number && 位运算
136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...
- Same binary weight (位运算)
题目描述 The binary weight of a positive integer is the number of 1's in its binary representation.for ...
- Leetcode 268 Missing Number 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
- 136. Single Number(位运算)
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 位运算 UEST 84 Binary Operations
题目传送门 题意:所有连续的子序列的三种位运算计算后的值的和的期望分别是多少 分析:因为所有连续子序列的组数有n * (n + 1) / 2种,所以要将他们分类降低复杂度,以ai为结尾的分成一组,至于 ...
- HDU 3006 The Number of set(位运算 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...
随机推荐
- 纪中10日T1 2313. 动态仙人掌
纪中10日 2313. 动态仙人掌 (File IO): input:dinosaur.in output:dinosaur.out 时间限制: 1500 ms 空间限制: 524288 KB 具 ...
- 将CGCS2000的坐标值转换为WGS84的坐标值
打开图层数据,或者将已有的Excel数据导入到ArcMap中,然后打开Toolbox, ArcToolbox --> Projections and Transformations --> ...
- Java求素数和
描述 从键盘任意输入两个整数m,n,编程计算并输出m~n之间的所有素数之和. 输入 在一行上输出m和n. 输出 m和n之间(包括m和n)的素数的和 难度 一般 输入示例 2 5 输出示例 10 完成代 ...
- Git的学习和使用
1.1. Git 了解git的仓库概念 熟悉何为版本控制,了解分布式版本控制(git)和集中式版本控制(svn) 能够熟练使用git的基本指令完成仓库的初始化/添加/提交/日志/回退/分支等操作 gi ...
- canvas-画圆心的算法
公式为x=16sin~3t,y=(13cost-5cos2t-2cos3t-cos4t) x+r(16Math.pow(Math.sin(t),3)) y-r(13Math.cos(t)-5Math. ...
- JS首字母进行分类合并加排序
let array = ['fds', 'ewfg1', 'cvd', 'ew', 'qer', 'jjh', 'rth', 'asd', 'vsd', 'tteh', 'fxv']; let map ...
- C# LINQ学习笔记三:LINQ to OBJECT之操作字符串
本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5814204.html,记录一下学习过程以备后续查用. 一.统计单词在字符串中出现的次数 请注意,若要执行计数, ...
- Python中io的open()在PyCharm环境下报错和路劲的问题
PS:我也是初学者,上班空闲时间学习学习Python.今天学到io的时候,遇到了两个用PyCharm环境编写代码的小白错误,如下: 两个问题都是如下代码: 1. 第一个问题:当写好代码之后,点击运行报 ...
- P问题,NP问题,NPC问题学习笔记
参考:https://www.luogu.org/blog/styx-ferryman/chu-sai-bei-kao-gan-huo-p-wen-ti-np-wen-ti-npc-wen-ti-sh ...
- 机器学习作业(一)线性回归——Python(numpy)实现
题目太长啦!文档下载[传送门] 第1题 简述:设计一个5*5的单位矩阵. import numpy as np A = np.eye(5) print(A) 运行结果: 第2题 简述:实现单变量线性回 ...