UVa729 - The Hamming Distance Problem(全排列)
来源:互联网 发布:美国用淘宝邮费很贵吗 编辑:程序博客网 时间:2024/06/14 15:07
给2个相同长度的2元字串,比较他们在相同位置的内容,并计算各位置内容不一样的总数,我们称该数為它们之间的Hamming distance。这任务可以经由对字串中各相同位置字元作XOR的运算或者做2进位的相加(但不进位)而得到。以下的例子為2个长度為10的2元字串A、B经过XOR运算。可以看出共有6个1,所以其Hamming distance為6。
A 0 1 0 0 1 0 1 0 0 0
B 1 1 0 1 0 1 0 1 0 0
A XOR B = 1 0 0 1 1 1 1 1 0 0
你的任务是给你字串的长度(N)及所要求的Hamming distance(H),请你输出所有这样的2元字串,也就是长度為N的二元字串,且恰好有H个1的字串。由数学我们得知这样的字串共有C(N,H)个。也就是:
N!
─────
(N-H)! H!
Input
输入的第一列有一个正整数,代表以下有多少组测试资料。
每组测试资料一列,含有2个正整数N、H(1 <= H <= N <= 16)。N代表字串的长度,H代表Hamming distance。
请参考Sample Input。
Output
对每一组测试资料,输出所有长度為N,且Hamming distance為H的二元字串,并由小到大输出。测试资料间请空一列。
Sample Input
2
4 2
3 2
Sample Output
0011
0101
0110
1001
1010
1100
011
101
110
code:
#include <stdio.h>int a[32];int main(){ int T, n, h; int i, tot, x, j; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&h); for(i=1;i<= 1<<n; i++) { for(j=0,x=i,tot=0;j<n;j++) { a[j] = x % 2; tot +=a[j]; x /=2; } if(tot == h) { for(j=n-1; j>=0; j--) printf("%d",a[j]); printf("\n"); } } if(T!=0) printf("\n"); } return 0;}
- UVa729 - The Hamming Distance Problem(全排列)
- UVa729 The Hamming Distance Problem
- uva729 The Hamming Distance Problem
- The Hamming Distance Problem UVA729
- UVA729 The Hamming Distance Problem
- uva729 The Hamming Distance Problem
- uva729 - The Hamming Distance Problem(Hamming距离问题)
- 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 (枚举排列)
- 729--The Hamming Distance Problem
- JOJ1466:The Hamming Distance Problem
- 729 - The Hamming Distance Problem
- 729 - The Hamming Distance Problem
- Linux Shell编程入门教程(新手必备)
- DOS命令之for
- 简单的程序诠释C++ STL算法系列之四:adjacent_find
- C++指针详解char * a, char ** a, char * a[], char a[][], char * a[][], char ** a[][], char * a [][][]
- 电子信息工程四年学习之思
- UVa729 - The Hamming Distance Problem(全排列)
- 搭建Window环境下Jni开发步骤(2)
- 杭电ACM 1002
- 使用const_cast的时机
- JavaScript学习笔记(二十九) method()方法
- 如何制作一款HTML5 RPG游戏引擎——第三篇,利用幕布切换场景
- 数据挖掘之预测建模(决策树)
- Unity3d C# 脚本学习小结(四)【怎么设置材质的Png图片部分透明】
- 银行线程---某银行有3业务受理窗口,每天办理业务100人次,输出业务办理过程。