Binary Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1287    Accepted Submission(s): 807

Problem Description
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.
 
Input
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.
 
Output
For each test case you should output n lines, each of which contains the result for each query in a single line.
 
Sample Input
2
2 5
1
2
1
2
3
4
5
5 2
1000000
9999
1423
3421
0
13245
353
 
Sample Output
1
2
1
1
1
9999
0

AC代码例如以下:

#include <stdio.h>
int a[105];
int count(int x)
{
int c = 0;
for(;x;x>>=1) if(x&1) c++;
return c;
}
int main()
{
int b, i, j, n, m, k, min, t,cases;
scanf("%d",&cases);
while(cases--)
{
scanf("%d%d",&n,&m);
for(i=0; i<n; i++) scanf("%d",&a[i]);
for(i=0; i<m; i++)
{
scanf("%d",&b);
min = count(b^a[0]);
k = 0;
for(j=1; j<n; j++)
{
t = count(b^a[j]);
if(t<min||t==min&&a[j]<a[k])
{ min = t;k = j;}
}
printf("%d\n",a[k]);
}
}
return 0;
}

杭州电 3711 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. [HDU] 3711 Binary Number [位运算]

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

  3. hdu 3711 Binary Number(暴力 模拟)

    Problem Description For non-negative integers x and y, f(x, y) , )=,f(, )=, f(, )=. Now given sets o ...

  4. hdu 1290 竭诚为杭州电礼物50周年

    专门为杭州电50周年礼事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  5. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列

    B. Fi Binary Number     A Fi-binary number is a number that contains only 0 and 1. It does not conta ...

  6. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

  7. 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)

    链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  8. 693. Binary Number with Alternating Bits - LeetCode

    Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...

  9. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

随机推荐

  1. python基础教程_学习笔记1:序列-1

    序列 数据结构:通过某种方式组织在一起的数据元素的集合,这些数据元素能够是数字或者字符,甚至能够是其它数据结构. python中,最主要的数据结构是序列. 序列中的每一个元素被分配一个序号--即元素的 ...

  2. mooon编译系统介绍(可复用Makefile)

    mooon编译系统介绍(可复用Makefile).pdf(ChinaUnix下载) CSDN下载:http://download.csdn.net/detail/aquester/5626929 mo ...

  3. Servlet和JSP读书笔记(一)

    Java Servlet 技术,简称Servlet,是Java中用于开发web应用程序的基本技术. Servlet实际上也就是一个Java程序.一个Servlet应用程序通常包含很多Servlet.而 ...

  4. Spring常见面试问题 (转)

    Spring 1. Spring工作机制及为什么要用?Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.Spring既是一个AOP框架,也是一IOC容器.SpringFramew ...

  5. Java基础之数组序列化、反序列化 小发现(不知道 是不是有问题)

    结论:  数组,无论是否声明为transient,都是可以序列化.反序列化的. 测试情况如下: 1.两种类型的数组:int .String: 2 声明为transient  或者不做任何修饰:. 3. ...

  6. newlisp 注释生成文档

    最近写了一个newlisp_armory库,用来实现一些newlisp自身不支持的操作.比如跨windows和ubuntu的目录拷贝功能等. 自己用的时候,发现没有API reference文档参考, ...

  7. ZOJ 3542 2011大连现场赛D题(简单模拟)

    Hexadecimal View Time Limit: 2 Seconds       Memory Limit: 65536 KB Hexadecimal is very important an ...

  8. Java Word Ladder(字梯)

    问题: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  9. 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注冊模块(附完整源代码)

    近期看老罗视频,做了一个简单的用户注冊系统.用户通过网页(JSP)输入用户名.真名和password,Servlet接收后通过JDBC将信息保存到MySQL中.尽管是个简单的不能再简单的东西,但麻雀虽 ...

  10. mysql导出和导入数据库

    出口 在dos计划,切换到mysql按照该文件夹bin下一个.输入以下命令 mysqldump -u root -p nxu_life > nxu_life2.sql 运行完毕后,就能够看到在b ...