首页 > 其他 > 详细

usaco Hamming Codes

时间:2015-09-03 14:02:49      阅读:272      评论:0      收藏:0      [点我收藏+]

 题目很简单,找出N个二进制下长度为B的,且两两之间二进制位上至少有D位不同

  暴力枚举就好了 

/*
ID: modengd1
PROG: hamming
LANG: C++
*/

#include <iostream>
#include <stdio.h>
#include <memory.h>

using namespace std;
int sta[64];
bool slove(int sta[64],int N,int B,int D,int deep)
{
    if(deep==N)
    {
        bool isbegin=true;
        for(int i=0;i<N;i++)
        {
            if(isbegin)
            {
                cout<<sta[i];
                isbegin=false;
            }
            else
                cout<<‘ ‘<<sta[i];
            if((i%10)==9)//每十个数字换行
            {
                isbegin=true;
                cout<<endl;
            }
        }
        if((N%10))//检查是否需要在最后一行输出换行
            cout<<endl;
        return true;
    }
    for(int i=0;i<(1<<B);i++)
    {
        bool allOk=true;
        sta[deep]=i;
        for(int j=0;j<deep;j++)
        {
            int dif=i^sta[j];
            int counter=0;
            while(dif>0)
            {
                dif-=dif&-dif;
                counter++;
            }
            if(counter<D)//汉明距离小于D就break
            {
                allOk=false;
                break;
            }
        }
        if(allOk&&slove(sta,N,B,D,deep+1))//和前面得到的数的汉明距离都足够的话再枚举下一个
                return true;
    }
    return false;
}
int main()
{
    freopen("hamming.in","r",stdin);
    freopen("hamming.out","w",stdout);
    int N,B,D;
    int sta[64];
    scanf("%d%d%d",&N,&B,&D);
    slove(sta,N,B,D,0);
    return 0;
}

  

usaco Hamming Codes

原文:http://www.cnblogs.com/modengdubai/p/4780097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!