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,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位的“Hamming距离”(1 <= D <= 7)。“Hamming距离”是指对于两个编码,他们二进制表示法中的不同二进制位的数目。看下面的两个编码 0x554 和 0x234(0x554和0x234分别表示两个十六进制数):
0x554 = 0101 0101 0100
0x234 = 0010 0011 0100
不同位 xxx xx
因为有五个位不同,所以“Hamming距离”是 5。
格式
PROGRAM NAME: hamming
INPUT FORMAT:
(file hamming.in)
一行,包括 N, B, D。
OUTPUT FORMAT:
(file hamming.out)
N 个编码(用十进制表示),要排序,十个一行。如果有多解,你的程序要输出这样的解:假如把它化为2进制数,它的值要最小。
SAMPLE INPUT
16 7 3
SAMPLE OUTPUT
0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127
/*
ID:******
PROG:hamming
LANG:C++
*/
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
int N;//结果数量
int B;//数字倍数
int D;//数码间距
int final[65];//存储结果集
int count_now = 0;//结果集现有元素数量
//判断两数字的汉明码距离是否符合要求
bool judge_leng(int a,int b){
int temp = 0;
for(int i = 0;i < B;i ++){
if((a & 1) != (b & 1))
temp ++;
a = a >> 1;
b = b >> 1;
}
if (temp >= D)
return true;
else
return false;
}
//判断该数字是否与已有数字相容
bool judge_suit(int a){
int symbol = 1;
for(int i = 0;i < count_now;i ++)
if(!judge_leng(a ,final[i])){
symbol = 0;
break;
}
if(symbol)
return true;
else
return false;
}
int main(){
ofstream fout ("hamming.out");
ifstream fin ("hamming.in");
fin >> N >> B >> D;
int end ;
end = (int)pow(2.0, B) - 1;
for(int j = 0; j <= end; j ++){
if(count_now == N)
break;
if(judge_suit(j))
final[count_now ++] = j;
}
int cc = 0;
while(count_now >= 10){
for(int i = 0; i < 9;i ++)
fout << final[cc ++] <<" ";
fout << final[cc ++] << endl;
count_now -= 10;
}
if(count_now){
for(int i = 0; i < count_now - 1; i ++)
fout << final[cc ++]<<" ";
fout << final[cc] << endl;
}
return 0;
}