USACO Section 2.1 Hamming Codes

本文介绍了一种基于暴力搜索的算法实现,该算法用于寻找一组特定数量的数,这些数之间的汉明距离至少为给定值。通过逐位比较二进制表示来计算汉明距离,并使用递增的方法检查每个可能的数是否满足条件。

暴力

 

 1 /* ID:linyvxi1
2 PROB:hamming
3 LANG:C++
4 */
5 #include <stdio.h>
6 #include <math.h>
7 #include <stdlib.h>
8 int N,B,D;
9 int max_search_num;
10 int final_result[130];
11 int num_found;
12
13 bool check(int next)
14 {
15 int s=0;
16 for(;s<num_found;s++){
17 int temp;
18 temp=next^final_result[s];
19 int count=0;
20 int i;
21 for(i=0;i<B;i++){
22 int out_num=temp&0x00000001;
23 if(out_num){
24 count++;
25 }
26 temp=temp>>1;
27 }
28 if(count<D){
29 return false;
30 }
31 }
32 return true;
33 }
34 int main()
35 {
36 freopen("hamming.in","r",stdin);
37 freopen("hamming.out","w",stdout);
38 scanf("%d%d%d",&N,&B,&D);
39 max_search_num=(int)pow(2,B)-1;
40 final_result[0]=0;
41 int i;
42 num_found=1;
43 for(i=1;i<=max_search_num;i++){
44 if(num_found==N){
45 break;
46 }
47 if(check(i)){//如果hamming距离符合要求
48 final_result[num_found++]=i;
49 }
50 }
51 int already_output=0;
52 for(i=0;i<num_found;i++){
53 printf("%d",final_result[i]);
54 already_output++;
55 if(already_output%10==0){
56 putchar('\n');
57 }else if(i!=num_found-1){
58 putchar(' ');
59 }else{
60 putchar('\n');
61 }
62 }
63 }



转载于:https://www.cnblogs.com/yangce/archive/2012/03/06/2382533.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值