腾讯坑爹的面试给了个汉明距离,让我不知所以
查了一下,居然是
“对于二进制字符串的a和b,海明距离为等于在a XOR b运算结果中1的个数(普遍算法)”
害我写了那么长的代码,以为要转成汉明码呢。。。。数了数不清的校验位和数值位。。
只要短短几行代码就可以了。。。
int Hammingdist(const int &ff, const int &ss)
{
int count = 0, f = ff, s = ss;
while(f != 0 && s != 0)
{
if(f % 2 != s % 2)
{
count ++;
}
f /= 2;
s /= 2;
}
while(f != 0)
{
if(f % 2 != 0)
{
count ++;
}
f /= 2;
}
while(s != 0)
{
if(s % 2 != 0)
{
count ++;
}
s /= 2;
}
return count;
}