杭州电 3711 Binary Number

杭州电 3711 Binary Number

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116703.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SVN提示update更新成功,但是本地文件却没有更新

    SVN提示update更新成功,但是本地文件却没有更新

    2021年10月20日
  • I2C电平转换电路_双向电平转换电路工作原理

    I2C电平转换电路_双向电平转换电路工作原理本文分享下I2C双向电平转换电路的设计原理,以及需要注意的事项。在I2C主从设备对接时,需要考虑主从设备的电平情况,常规的主要有3种:5V,3.3V,1.8V。如果电平相同,比如都是3.3V,那么可以直接对接。如果电平不同,一个高电平是3.3V,另外一个是1.8V,那么就需要接入其它的器件来做一下电平转换,通常是接入NMOS管。如上图,此图来源于I2C官方协议,协议标准文件里…

    2022年8月10日
  • (7)JMeter元件详解之 Interleave Controller 交替控制器

    (7)JMeter元件详解之 Interleave Controller 交替控制器

    2021年7月14日
  • 【c语言】小游戏程序——弹跳小球

    现在说一下其中一个最简单的小程序:弹跳小球———————————————LINE————————————————首先我们知道,在窗口的坐标系原点是在窗口左上角的,如图所示然后我们如果想在这个坐标系里面的某个点上画出一个小球,那么它的坐标系应该就是这样的转换到c语言的思维的话:X0既是打印…

    2022年4月3日
  • ACM中Java输入输出[通俗易懂]

    ACM中Java输入输出[通俗易懂]最初写算法时,是用Scanner的。因为当时接触的测试数据基本都是以算法的复杂度为主,但是后面遇到大量的输入数据时。发现Scanner远远不能满足条件。下面列出几种常用的输入输出方式。(输出统一用printwriter,系统的system.out太慢,结尾要释放缓存才能输出,不然数据放在缓存中输不出来)1:Scanner这个类最慢,但是最好用,因为这个类没有缓存处理,所以io方面大量输入…

    2022年5月18日
  • 2018年最新Java面试题及答案整理

    2018年最新Java面试题及答案整理下列面试题都是在网上收集的,本人抱着学习的态度找了下参考答案,有不足的地方还请指正,更多精彩内容可以关注我的微信公众号:Java团长基础篇基本功面向对象特征封装,继承,多态和抽象 封装 封装给对象提供了隐藏内部特性和行为的能力。对象提供一些能被其他对象访问的方法来改 变它内部的数据。在Java当中,有3种修饰符:public,private和prote…

    2022年6月18日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号