Blog·Tanky WooABOUTRSS

HDU/HDOJ 3711 Binary Number

20 Jan 2011
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3711

郁闷,WA了7次。始终不知道哪错了,后来才发现忽略了这句话:"If there are more than one such integer in set A, choose the smallest one."

#include 
#include 
using namespace std;

int f(long a, long b)
{
    int cnt = 0;
    while (a || b)
    {
        int m = a&1;
        int n = b&1;
        if(m != n)
            cnt++;
        a /= 2;
        b /= 2;
    }
    return cnt;
}
int main()
{
    //freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    int t, m, n, minn;
    long minx;
    long a[200], b[200];
    cin >> t;
    while(t--)
    {
        cin >> m >> n;
        for(int i=1; i<=m; ++i)
            cin >> a[i];
        sort(a+1, a+m+1);
        for(int i=1; i<=n; ++i)
            cin >> b[i];
        for(int i=1; i<=n; ++i)
        {
            minn = 99999;
            minx = 0;
            for(int j=1; j<=m; ++j)
                if(minn > f(a[j], b[i]))
                {
                    minn = f(a[j], b[i]);
                    minx = a[j];
                }
            cout << minx << endl;
        }
    }
}