zoukankan      html  css  js  c++  java
  • hdu 3711 Binary Number(暴力 模拟)

    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
     
    Source
     
     
     
    法一:求出b[i]、a[j]的二进制数后,再比较统计
     
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cmath>
     7 #include<stdlib.h>
     8 #include<map>
     9 using namespace std;
    10 #define N 106
    11 int n,m;
    12 int a[N];
    13 int b[N];
    14 int s[1000];
    15 int k1;
    16 
    17 void change(int x){
    18     memset(s,0,sizeof(s));
    19     k1=0;
    20     
    21     while(x){
    22         s[k1++]=x%2;
    23         x/=2;
    24     }
    25     
    26 }
    27 int s1[1000];
    28 int k2;
    29 void change1(int x){
    30     memset(s1,0,sizeof(s1));
    31     k2=0;
    32     
    33     while(x){
    34         s1[k2++]=x%2;
    35         x/=2;
    36     }
    37     
    38 }
    39 int main()
    40 {
    41     int t;
    42     scanf("%d",&t);
    43     while(t--){
    44         scanf("%d%d",&n,&m);
    45         for(int i=0;i<n;i++){
    46             scanf("%d",&a[i]);
    47         }
    48         for(int i=0;i<m;i++){
    49             scanf("%d",&b[i]);
    50         }
    51         sort(a,a+n);
    52         for(int i=0;i<m;i++){
    53             change(b[i]);
    54             int minn=1000000000;
    55             int f=0;
    56             for(int j=0;j<n;j++){
    57                 change1(a[j]);
    58                 int ans=0;
    59                 int q=max(k1,k2);
    60                 for(int w=0;w<q;w++){
    61                     if(s[w]!=s1[w]){
    62                         ans++;
    63                     }
    64                 }
    65                 if(ans<minn){
    66                     minn=ans;
    67                     f=j;
    68                 }
    69             }
    70             printf("%d
    ",a[f]);
    71         }
    72     }
    73     return 0;
    74 }
    View Code

    法二:先求b[i]^a[j],再一次性统计

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<set>
     5 #include<map>
     6 #include<vector>
     7 #include<algorithm>
     8 using namespace std;
     9 #define ll long long 
    10 #define N 50006
    11 int a[106];
    12 int b[106];
    13 int n,m;
    14 int s1[1000];
    15 int solve(int x)
    16 {
    17     int k1=0;
    18     while(x)
    19     {
    20         s1[k1++]=x%2;
    21         x=x/2;
    22     }
    23     int ans=0;
    24     for(int i=0;i<k1;i++)
    25     {
    26         if(s1[i]==1)
    27          ans++;
    28     }
    29     return ans;
    30 }
    31 int main()
    32 {
    33     int t;
    34     scanf("%d",&t);
    35     while(t--)
    36     {
    37         scanf("%d%d",&n,&m);
    38         for(int i=0;i<n;i++)
    39         {
    40             scanf("%d",&a[i]);
    41         }
    42         for(int i=0;i<m;i++)
    43         {
    44             scanf("%d",&b[i]);
    45             int minn=100000000;
    46             int flag;
    47             for(int j=0;j<n;j++)
    48             {
    49                 int tmp=solve(b[i]^a[j]);
    50                 //printf("---%d
    ",b[i]^a[j]);
    51                 //printf("%d
    ",tmp);
    52                 if(minn>tmp)
    53                 {
    54                     minn=tmp;
    55                     flag=a[j];
    56                 }
    57                 else if(minn==tmp)
    58                 {
    59                     if(flag>a[j])
    60                     {
    61                         flag=a[j];
    62                     }
    63                 }
    64             }
    65             printf("%d
    ",flag);
    66         }
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    FRAM在智能电子式电表中的应用
    element UI实现动态生成多级表头
    小微信小程序开发相关网站
    响应式开发bootstrap
    媒体查询
    前端导出功能实现的两种方式
    watch监听对象遇坑
    vue中使用v-for时为什么要用到key?为什么不能用index作为key?
    vue中我改变了data中的一个值,但现在视图上没有实时更新,请问我怎么拿到更新后的值?
    python 链接数据库的模块
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4799147.html
Copyright © 2011-2022 走看看