SDNU 1305.Hamming Codes 位运算


1305.Hamming Codes


Description

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.


Input

N, B, D on a single line


Output

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 Input

16 7 3

Sample Output

0 7 25 30 42 45 51 52 75 76

82 85 97 102 120 127


    这题一开始也读了好久才读懂,输入N,B,D,N代表需要的数字个数,B代表数字的长度,D代表两个数之间的海明码至少要相差多少。而海明码就是指的把两个数化成二进制的形式,然后比较有几位是不相同的,有几位不同,海明码就是多少。简单说就是把两个数字异或,二进制形式有几个1海明码就是几。输出的一行数字任取两个海明码都要大于等于D,所以确定数字的时候要把前面的数字全部进行一遍比较,抱着试试的心态先暴力了一下,成功AC了......

    下面AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
int a[1000005];

int main()
{
     int N,B,D;
     int i,j;
     int k,t;
     int cou;
     while(scanf("%d%d%d",&N,&B,&D)!=EOF)
     {
          memset(a,0,sizeof(a));
          k=1;
          a[1]=pow(2,D)-1;
          cout<<0<<" "<<a[1];
          for(i=2;i<N;k++)
          {
               for(j=0;j<i;j++)
               {
                    t=k^a[j];
                    cou=0;
                    while(t>0)
                    {
                         if((t&1)==1)
                              cou++;
                         if(cou>=D)
                              break;
                         t>>=1;
                    }
                    if(cou<D)
                         break;
               }
               a[i]=k;
               if(j==i)
               {
                    if(i%10==0)
                    {
                         cout<<endl<<a[i];
                    }
                    else
                    {
                         cout<<" "<<a[i];
                    }
                    i++;
               }
          }
     }
     return  0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值