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. ...
随机推荐
- Vue中vue-i18n结合vant-ui实现国际化
(一)添加依赖模块 在package.json文件中添加vant模块的依赖,如: // package.json { "dependencies": { "vant&qu ...
- 如何将旧Mac的数据迁移到新的MacBook Pro?
最新版的MacBook Pro已经上市,具有超凡魅力的Touch Bar开创了一个新时代.苗条的设计和华丽的显示效果也起到了推动运动的作用……!将数据从旧Mac传输到新Mac不再是一件漫长的事.您只需 ...
- PHP0004:PHP基础3
php写在 标签里
- [NOI2003]文本编辑器 [Fhq Treap]
[NOI2003]文本编辑器 没啥好说的 就是个板子 #include <bits/stdc++.h> // #define int long long #define rep(a , b ...
- 安装Logstash到linux(源码)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:logstash-7.1.0 硬件要求:最低2核4GB 安装过程 1.源码安装JDK 1.1.从 ...
- LeetCode 112. 路径总和 (递归遍历二叉树)
题目链接:https://leetcode-cn.com/problems/path-sum/ 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- PAT (Basic Level) Practice (中文)1021 个位数统计 (15 分)
给定一个 k 位整数 1 (0, ,, dk−1>0),请编写程序统计每种不同的个位数字出现的次数.例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3. 输入格式: 每个输入包含 ...
- Npoi常用操作方法介绍
1.ShiftRows(startRow,endRow,moveRows) 将开始行到结束行向上或者向下移动moveRows行,moveRows为正数向下移动,为负数向上移动(向上移动,会把之前的行覆 ...
- CSS 美化网页元素
一.为什么使用CSS 1.有效的传递页面信息 2.使用CSS美化过的页面文本,使页面漂亮.美观,吸引用户 3.可以很好的突出页面的主题内容,使用户第一眼可以看到页面主要内容 4.具有良好的用户体验 二 ...
- C++野指针的存在方式和误区
1. char* x;这样的一定是野指针,指针声明时要直接初始化!或者置null也行! 2. int main() { char *x=new char; delete x; cout<< ...