Hamming Distance(随机算法)

本博客介绍了一个通过随机数产生的十六进制数进行异或运算,统计异或结果中1的个数,并找出最小值的方法。

http://acm.hdu.edu.cn/showproblem.php?pid=4712

题意:计算任意两个十六进制的数异或后1的最少个数。

思路:用随机数随机产生两个数作为下标,记录这两个数异或后1的个数,输出1的个数最少是多少。

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <time.h>
 4 #include <math.h>
 5 
 6 using namespace std;
 7 const int N=100002;
 8 const int INF=1<<29;
 9 int f[N];
10 int main()
11 {
12     int t;
13     scanf("%d",&t);
14     while(t--)
15     {
16         int n,ans = INF;
17         scanf("%d",&n);
18         for (int i = 0; i < n; i ++)
19             scanf("%x",&f[i]);
20         srand((unsigned)time(NULL));
21         for (int i = 0; i < N*10; i ++)
22         {
23             int a = rand()%n;
24             int b = rand()%n;
25             if (a==b)
26                 continue ;
27             int t = f[a]^f[b];
28             int temp = __builtin_popcount(t);
29             ans = min(ans,temp);
30         }
31         printf("%d\n",ans);
32     }
33     return 0;
34 }
View Code

 

转载于:https://www.cnblogs.com/lahblogs/p/3319723.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值