usaco

来源:互联网 发布:淘宝旺旺号怎么加好友 编辑:程序博客网 时间:2024/06/06 08:52

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 0100Bit 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 7682 85 97 102 120 127
思路:一个函数判断汉明码位数,一个判断成立与否
/*ID:PROG: hammingLANG: C++*/#include<iostream>#include<cstdio>#include<fstream>using namespace std;int arr[100],count1=1;int n,b,d;int judge(int x,int y){    int num=0;    for(int i=0;i<b;i++){        int a=x&1;        int c=y&1;        if(a!=c)num++;        x>>=1;y>>=1;    }    return num;}int judge1(int m,int k){//数m,k个数已经存入    for(int i=1;i<=k;i++){        if(judge(arr[i],m)<d)return 0;    }    return 1;}int main(){    freopen("hamming.in","r",stdin);    freopen("hamming.out","w",stdout);    cin>>n>>b>>d;    arr[1]=0;    for(int i=1;i<=1<<(b+1)-1;i++){//0肯定已经加入,所以从1到b为的最大的数一个一个判断        if(count1>=n)break;        if(judge1(i,count1))arr[++count1]=i;//如果判断成立,则存入arr,count1++    }    for(int i=1;i<n;i++){        cout<<arr[i];        if(i%10==0)cout<<endl;        else cout<<" ";    }    cout<<arr[n]<<endl;}
我学习了:1<<(b+1)-1判断b位二进制最大的数
二进制x&1为末位数的值


原创粉丝点击