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.
N, B, D on a single line
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.
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;
}