首页 > 代码库 > Section 2.1.Hamming Codes (hamming)
Section 2.1.Hamming Codes (hamming)
//题目的链接http://www.nocow.cn/index.php/Translate:USACO/hamming
/*【位运算】+【异或操作】*/
/*
ID: zhangsh35
PROG: hamming
LANG: C++
*/
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
const int MAXN=64;
int ham[MAXN];
int tot,Max,dis;
int cnt=1;
bool judge(int num);
#define LOCAL
int main()
{
#ifdef LOCAL
freopen("hamming.in","r",stdin);
freopen("hamming.out","w",stdout);
#endif
cin>>tot>>Max>>dis;
int i,j;
ham[0]=0;
cout<<"0 ";
for(i=1;i<=pow(2,Max)-1;i++)
{
if(judge(i))
{
ham[cnt++]=i;
cout<<i;
if(cnt==tot)
{
cout<<endl;
break;
}
else if(!(cnt%10))
cout<<endl;
else
cout<<" ";
}
}
}
bool judge(int num)
{
int i,tot;
bool ok=true;
for(i=0;i<cnt;i++)
{
tot=0;
int data=http://www.mamicode.com/num xor ham[i];
while(data)
{
tot++;
data&=data-1;//计算一个数的二进制位中1的个数,涨知识了。
}
if(tot<dis)
{
ok=false;
break;
}
}
return ok;
}
Section 2.1.Hamming Codes (hamming)