首页 > 其他 > 详细

Section 2.1.Hamming Codes (hamming)

时间:2014-12-14 18:47:16      阅读:314      评论:0      收藏:0      [点我收藏+]

//题目的链接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=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)

原文:http://zhangsh.blog.51cto.com/9684448/1589727

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!