Hamming Codes(二进制枚举)

Hamming Codes
Rob Kolstad

Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of length B bits (1 <= B <= 8), such that each of the codewords is at least Hamming distance of D (1 <= D <= 7) away from each of the other codewords. The Hamming distance between a pair of codewords is the number of binary bits that differ in their binary notation. Consider the two codewords 0x554 and 0x234 and their differences (0x554 means the hexadecimal number with hex digits 5, 5, and 4):

        0x554 = 0101 0101 0100
        0x234 = 0010 0011 0100
Bit differences: xxx  xx

Since five bits were different, the Hamming distance is 5.

PROGRAM NAME: hamming

INPUT FORMAT

N, B, D on a single line

SAMPLE INPUT (file hamming.in)

16 7 3

OUTPUT FORMAT

N codewords, sorted, in decimal, ten per line. In the case of multiple solutions, your program should output the solution which, if interpreted as a base 2^B integer, would have the least value.

SAMPLE OUTPUT (file hamming.out)

0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127

 

    题意:

    给出N(1 ~ 64),B(1 ~ 8),D(1 ~ 7),然后输出 N 个B 位二进制的数,这些数任意两者之间化为二进制后,不同的数至少要有D 个,该序列为和最小的序列。

 

    思路:

    枚举。最多8位二进制,说明最大才255,果断二进制枚举,1A。

 

    AC:

/*    
TASK:hamming    
LANG:C++    
ID:sum-g1    
*/
#include<stdio.h>
#include<math.h>
#include<string.h>

int n,b,d,ans;
int bin_a[10],bin_b[10];
int fin[70];

int test(int num)
{
    int k = 0;
    while(num)
    {
        k++;
        bin_a[k] = num % 2;
        num /= 2;
    }
    for(int i = 1;i <= ans;i++)
    {
        int change = fin[i];
        k = 0;
        memset(bin_b,0,sizeof(bin_b));
        while(change)
        {
            k++;
            bin_b[k] = change % 2;
            change /= 2;
        }

        int sum = 0,temp = 0;
        for(k = 1;k <= b;k++)
        {
            if(bin_a[k] != bin_b[k]) sum++;
            if(sum >= d)
            {
                temp = 1;
                break;
            }
        }
        if(!temp) return 0;
    }
    return 1;
}

int main()
{
    freopen("hamming.in","r",stdin);        
    freopen("hamming.out","w",stdout); 
    ans = 1;
    scanf("%d%d%d",&n,&b,&d);
    fin[1] = 0;
    printf("%d ",fin[1]);
    for(int i = 1;i <= pow(2,b) - 1;i++)
    {
        memset(bin_a,0,sizeof(bin_a));
        if(test(i))
        {
            ans++;
            fin[ans] = i;
            printf("%d",i);
            if(!(ans % 10) || ans == n) printf("\n");
            else                        printf(" ");
        }
        if(ans == n) break;
    }
    return 0;
}
   

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值