zoukankan      html  css  js  c++  java
  • [HDOJ3711]Binary Number(枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3711

    题意:两个数集合,找二进制下位数不同最少的数,如果一样,找集合数最小的。

    暴力枚举

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn = 110;
     5 int n, m;
     6 int a[maxn], b[maxn];
     7 
     8 int ok(int x, int y) {
     9   int xx = x, yy = y;
    10 
    11   int cnt = 0;
    12   if(x > y) swap(x, y);
    13   while(x) {
    14     if((x&1)!=(y&1)) cnt++;
    15     x >>= 1; y >>= 1;
    16   }
    17   while(y) {
    18     if(y&1) cnt++;
    19     y >>= 1;
    20   }
    21   return cnt;
    22 }
    23 
    24 int main() {
    25   //freopen("in", "r", stdin);
    26   int T;
    27   scanf("%d", &T);
    28   while(T--) {
    29     scanf("%d %d", &n, &m);
    30     for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    31     for(int i = 1; i <= m; i++) scanf("%d", &b[i]);
    32     for(int i = 1; i <= m; i++) {
    33       int ret = 9000000, k;
    34       for(int j = 1; j <= n; j++) {
    35         int tmp = ok(b[i], a[j]);
    36         if(ret > tmp) {
    37           ret = tmp;
    38           k = j;
    39         }
    40         else if(ret == tmp) {
    41           if(a[k] > a[j]) k = j;
    42         }
    43       }
    44       cout << a[k] << endl;
    45     }
    46   }
    47   return 0;
    48 }
  • 相关阅读:
    Python机器学习-分类
    Python2.x和Python3.x的区别
    cut命令
    uniq 命令
    sort命令
    KMP算法
    Trie树
    做10年Windows程序员与做10年Linux程序员的区别
    c语言内存模型
    C语言的一个关键字——static
  • 原文地址:https://www.cnblogs.com/kirai/p/5957973.html
Copyright © 2011-2022 走看看