UVa 729: The Hamming Distance Problem

本文介绍了一种通过递归枚举的方法解决特定编程问题的技巧,详细展示了如何从最左边一位开始,分别讨论数字为0和为1的情况,并通过代码实现来验证解决方案的有效性。

这道题只要枚举出所有情况就可以了。

从最左边一位开始分别讨论为0和为1 两种情况,向右递归。

我的解题代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

int s[16];
int T,N,H;

void f(int curn, int curh)
{
	if(curn==N && curh==H)
	{
		for(int i=0; i<N; i++) cout << s[i]; cout << endl;
		return ;
	}
	if((N-curn) > (H-curh))
	{
		s[curn]=0;
		f(curn+1,curh);
	}
	if(curh < H)
	{
		s[curn]=1;
		f(curn+1,curh+1);
	}
}

int main()
{
	cin >> T;
	while(T--)
	{
		cin >> N >> H;
		memset(s,0,sizeof(s));
		f(0,0);
		if(T) cout << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值