zoukankan      html  css  js  c++  java
  • ZOJ Problem Set 3418 Binary Number


    Time Limit: 2 Seconds      Memory Limit: 65536 KB


    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 integers 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

    Author: CAO, Peng
    Source: The 2010 ACM-ICPC Asia Chengdu Regional Contest

    关键点:两数抑或运算得到两数二进制表示时不同的位,通过位移和与运算统计两数不同在二进制表示时不同尾数的个数。

      1: #include<iostream>
    
      2: #include<vector>
    
      3: 
    
      4: using namespace std;
    
      5: /* 
      6:  * 寻找两个数以二进制表示时的不同位数的个数
    
      7: */
    
      8: unsigned  countDef(unsigned a,unsigned b){
    
      9:     int result = 0;
    
     10:         
    
     11:     for(int c = a^b; c > 0;c = c >> 1){
    
     12:         if(c&1 > 0){
    
     13:             result ++;
    
     14:         }
    
     15:     }
    
     16:     return result;
    
     17: }
    
     18: /* 
     19:  * 寻找A中最符合条件的数字a
    
     20: */
    
     21: unsigned findMinCountInA(const vector<unsigned>& set, unsigned b){
    
     22:     unsigned min = set[0];
    
     23:     unsigned count = countDef(set[0], b);
    
     24:     unsigned count_ = 0;
    
     25:     for(unsigned i = 1;i < set.size(); i++){
    
     26:         count_ = countDef(set[i], b);
    
     27:         if(count_ < count){
    
     28:             min = set[i];
    
     29:             count = count_;
    
     30:         }else if(count_ == count && set[i] < min){
    
     31:             min = set[i];
    
     32:         }
    
     33:     }
    
     34:     return min;
    
     35: }
    
     36: int main(){
    
     37:     unsigned cases = 0;
    
     38:     cin >> cases;
    
     39:     unsigned aSize = 0, bSize = 0;
    
     40:     while(cases--){
    
     41:         cin>>aSize>>bSize; 
    
     42:         vector<unsigned> aSet;//用来存放容器A中的数字
    
     43:         unsigned elemIna = 0;
    
     44:         while(aSize--){
    
     45:             cin>>elemIna;
    
     46:             aSet.push_back(elemIna);
    
     47:         }
    
     48:         unsigned elemInb = 0;
    
     49:         while(bSize--){
    
     50:             cin>>elemInb;
    
     51:             cout<<findMinCountInA(aSet, elemInb)<<endl;
    
     52:         }
    
     53:     }
    
     54: }
    
     55: 
  • 相关阅读:
    测试用例模板和编写目的
    使用AndroidStudio配置码云时,提醒following remotes are already on gitee git
    win10操作系统查看电池损耗,电池使用时间
    windows技巧03----win的一些组合键
    windows技巧02---------输入特殊字符
    windows技巧01------------记事本默认保存修改时间
    word2010如何让第一页不显示页码
    docker镜像管理基础
    docker基础用法
    SaltStack进阶
  • 原文地址:https://www.cnblogs.com/malloc/p/1987423.html
Copyright © 2011-2022 走看看