《ACM程序设计》例题解析-HDU3711

HDU 3711Binary Number

题目大意

§ f( a,b ) 是非负整数 a b 二进制时不同的位数( hamming 距离)
§ 给两个集合 A B A B 的大小不超过 100
§ 对于 B 中每个数 b ,求 A f( a,b ) 最小的数。

思路

¡ 直接暴力
¡ 对于 B 中的每个值,暴力枚举 A 中所有的值,记录下 f( a,b ) 最小的那个。

代码

#include <functional>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <cctype>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <cstdio>
#include <deque>

using namespace std;
 
#define PB push_back
#define MP make_pair
#define FU(i,n) for(i=0;i<n;i++)
#define FD(i,n) for(i=n-1;i>=0;i--)

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;

int cnt1(int x){
    int c = 0;
    for(;x;x>>=1) if(x&1) c++;
    return c;
}

int main(){
    int a[100],b,i,j,n,m,k,min,t,ca;
    cin >> ca;
    while(ca--){
        cin >> n >> m;
        FU(i,n) cin>>a[i];
        FU(i,m){
            cin>>b;
            min = cnt1(b^a[0]);
            k = 0;
            for(j=1;j<n;j++){
                t = cnt1(b^a[j]);
                if(t < min || t == min && a[j] < a[k]) min = t, k = j;
            }
            cout << a[k] << endl;
        }
        
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值