USACO 2.1.5 Hamming Codes

// hamming.cpp : 定义控制台应用程序的入口点。
//
/*
ID: maiyuet1
PROG: hamming
LANG: C++
*/
//#include "stdafx.h"
//暴力.....
#include <iostream>
#include <cstdio>
using namespace std;

const int MAX = 1<<9;
int N,B,D;

//判断有多少位不同
int Distance(int a, int b)
{
	int cnt = 0;
	int d;
	for(int i=0; i<B; i++)
	{
		d = 1 << i;
		if((a&d)^(b&d))
		{
			cnt++;
		}
	}
	return cnt;
}

int main()
{
	freopen("hamming.in","r",stdin);
	freopen("hamming.out","w",stdout);
	int ans[MAX];
	int sign;
	int k = 1;
	cin>>N>>B>>D;
	ans[1] = 0;
	for(int i=1; k<=N && i<=MAX; i++)
	{
		sign = 1;
		for(int j=1; j<=k; j++)
		{
			if(Distance(i,ans[j]) < D)
			{
				sign = 0;
				break;
			}
		}
		if(sign)
		{
			ans[++k] = i;
		}
	}
	for(int i=1; i<=N-1; i++)
	{
		if(i % 10 == 0)
		{
			cout<<ans[i]<<endl;
		}
		else
		{
			cout<<ans[i]<<" ";
		}
	}
	cout<<ans[N]<<endl;
	return 0;
}

你可能感兴趣的:(c,distance)