HDU/HDOJ 3711 Binary Number

传送门: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.”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <algorithm>
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;
        }
    }
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

发表评论

电子邮件地址不会被公开。 必填项已用*标注