UVA - 729 The Hamming Distance Problem (全排列)
来源:互联网 发布:肯德基 麦当劳 知乎 编辑:程序博客网 时间:2024/06/06 02:33
The Hamming Distance Problem
The Hamming distance between two strings of bits (binary integers) is the number of corresponding bit positions that differ. This can be found by using XOR on corresponding bits or equivalently, by adding corresponding bits (base 2) without a carry. For example, in the two bit strings that follow:
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
The Hamming distance (H) between these 10-bit strings is 6, the number of 1's in the XOR string.
Input
Input consists of several datasets. The first line of the input contains the number of datasets, and it's followed by a blank line. Each dataset containsN, the length of the bit strings and H, the Hamming distance, on the same line. There is a blank line between test cases.Output
For each dataset print a list of all possible bit strings of length N that are Hamming distanceH from the bit string containing all 0's (origin). That is, all bit strings of lengthN with exactly H1's printed in ascending lexicographical order.
The number of such bit strings is equal to the combinatorial symbol C(N,H). This is the number of possible combinations ofN-H zeros and H ones. It is equal to
This number can be very large. The program should work for .
Print a blank line between datasets.
Sample Input
14 2
Sample Output
001101010110100110101100
解析:给出两个数,一个是总位数,另一个是1的位数,求全排列
#include <iostream>#include <algorithm>#include <string.h>using namespace std;int main() {int t;int n,h;while(cin >> t) {while( t-- ) {cin >> n >> h;int a[30];memset(a,0,sizeof(a));for(int i = 0; i < n-h; i++) {a[i] = 0;}for(int i = n - h; i < n; i++) {a[i] = 1;}do {for(int i = 0; i < n; i++) {cout << a[i];}cout << endl;} while( next_permutation(a,a+n));if(t) {cout<<endl;}}}return 0;}
0 0
- uva 729 - The Hamming Distance Problem(全排列)
- UVA - 729 The Hamming Distance Problem (全排列)
- uva-729 - The Hamming Distance Problem-全排列
- UVA - 729 - The Hamming Distance Problem (枚举排列)
- UVa729 - 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
- 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
- UVa 729 - The Hamming Distance Problem
- 请求被忽略
- OC中类和对象的创建和使用
- Java1.5泛型指南中文版(Java1.5 Generic Tutorial)
- HDU 1072 Nightmare - BFS + 队列
- 选择运算符
- UVA - 729 The Hamming Distance Problem (全排列)
- 【Leetcode长征系列】Add Binary
- windows下捕获dump
- tableView编辑:插入、删除和移动
- Java布局管理器
- 操作系统复习——绪论
- 快速的回顾
- 黑马程序员——计算机语言概述
- HttpPost 流的使用方法