729 - The Hamming Distance Problem(可重集排列,直接枚举)
2014-03-02 20:05
316 查看
题目:729 - The Hamming Distance Problem
题目大意:给出字符串的长度,给出要求出现的1的个数,打印出所有的可能的排列,按照递增的顺序,这个字符串只有01组成。
解题思路:这题的思路也是生成可重集排列的思路,只是这里只有0,1,并且个数确定,所以在要进行递归的时候要进行判断。下一个要填0 的时候需要判断一下0的现有个数,1的也一样。如果现有的0(1)的个数小于要求的个数,就说明当前0(1)还可用就递归。
题目大意:给出字符串的长度,给出要求出现的1的个数,打印出所有的可能的排列,按照递增的顺序,这个字符串只有01组成。
解题思路:这题的思路也是生成可重集排列的思路,只是这里只有0,1,并且个数确定,所以在要进行递归的时候要进行判断。下一个要填0 的时候需要判断一下0的现有个数,1的也一样。如果现有的0(1)的个数小于要求的个数,就说明当前0(1)还可用就递归。
#include<stdio.h> const int N = 20; int s , n, h, t; void build(int cur, int * A) { int i, j; if(cur != n) { for( i = 0; i < 2; i++){ int c1 = 0; for(j = 0; j < cur; j++) if(A[j] == 0) c1++; if((c1 < n - h && !i) || (cur - c1 < h && i)) { A[cur] = i; build(cur + 1, A); } } } else { for(i = 0; i < n; i++) printf("%d", A[i]); printf("\n"); } } int main() { scanf("%d", &t); while(t--){ scanf("%d%d", &n, &h); int A ; build(0, A); if(t) printf("\n"); } return 0; }
相关文章推荐
- UVa 729 The Hamming Distance Problem【枚举排列】
- UVa 729 - The Hamming Distance Problem
- UVa 729 - The Hamming Distance Problem
- 729 - The Hamming Distance Problem
- UVa729 The Hamming Distance Problem
- UVA - 729 The Hamming Distance Problem
- 729 - The Hamming Distance Problem
- UVA - 729 The Hamming Distance Problem
- uva 729 The Hamming Distance Problem
- uva 729 - The Hamming Distance Problem 枚举排列
- UVa-729-The Hamming Distance Problem
- uva 729 - The Hamming Distance Problem
- UVA 729 The Hamming Distance Problem
- 729 - The Hamming Distance Problem
- UVa-729-The Hamming Distance Problem
- UVA 729 - The Hamming Distance Problem
- UVa 729 The Hamming Distance Problem (全排列输出&小细节)
- UvaOJ 729 The Hamming Distance Problem
- UVA 729 The Hamming Distance Problem
- UVa729 - The Hamming Distance Problem(全排列)