杭州电 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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 绑定运行计划sql_plan_baseline[通俗易懂]

    绑定运行计划sql_plan_baseline

    2022年1月23日
    56
  • Maven聚合_maven地址

    Maven聚合_maven地址rt,常用Maven配置整合,不定期更新dependenciesjstl开发环境servlet开发环境mybatismysql驱动包jacksonpagehelperlog4jshi

    2022年8月16日
    6
  • qt tabwidget使用_word横向表格变竖向

    qt tabwidget使用_word横向表格变竖向QTabWidget竖向QTabBar横向

    2022年9月23日
    0
  • linux 内核 – ioctl 函数详解

    linux 内核 – ioctl 函数详解1.概念ioctl是设备驱动程序中设备控制接口函数,一个字符设备驱动通常会实现设备打开、关闭、读、写等功能,在一些需要细分的情境下,如果需要扩展新的功能,通常以增设ioctl()命令的方式实现。在文件I/O中,ioctl扮演着重要角色,本文将以驱动开发为侧重点,从用户空间到内核空间纵向分析ioctl函数。2.用户空间ioctl#include&…

    2022年10月18日
    2
  • 【获奖公布】“我的2016”主题征文活动

    【获奖公布】“我的2016”主题征文活动还记得2015的年末,2016的新年伊始,你给自己定下的目标,对自己许下的诺言么?时光荏苒,一年又在指缝间溜走了,离2016的结束还剩十多天,在接下来的这十几天里,让我们用博客记录下这或开心、或痛苦,或特殊,或平淡的2016年,愿剩下的每一天我们都不会虚度~值此岁末之际,CSDN博客频道携手“图灵教育”开展了“我的2016”主题征文活动,听大家聊聊2016年的工作、生活中的点滴感动、喜悦和收获,

    2022年6月21日
    23
  • Ubuntu 定时执行脚本

    Ubuntu 定时执行脚本一、关于crontabcron是一个Linux定时执行工具,可以在无需人工干预的情况下运行作业。在Ubuntu中,cron是被默认安装并启动的。二、例子直接上例子,来看看怎么用。需求:定时每天8点,自动执行保存在/root目录下hello.sh脚本1、方法很简单,只需编辑ect下crontab文件就行了,这个文件里存放的就是cron要执行的命令,以及定时执行的时间…

    2022年7月17日
    39

发表回复

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

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