UVA 729 - The Hamming Distance Problem 解题报告

The description

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 addingcorresponding bits (base 2) without a carry. For example, in the two bitstrings that follow:

         A0 1 0 0 1 0 1 0 0 0 

         B1 1 0 1 0 1 0 1 0 0 

 AXOR B = 1 0 0 1 1 1 1 1 0 0

The Hamming distance (H) between these10-bit strings is 6, the number of 1's in the XOR string.

Input

Input consists of several datasets. The firstline of the input contains the number of datasets, and it's followed by a blankline. Each dataset contains N, the length of the bit strings and H,the Hamming distance, on the same line. There is a blank line between testcases.

Output

For each dataset print a list of all possible bitstrings of length N that are Hamming distance Hfromthe bit string containing all 0's (origin). That is, all bit strings of length N withexactlyH 1'sprinted 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 of N-H zerosand H ones. It is equal to


This number can be very large. The program shouldwork for .

Print a blank line between datasets.

Sample input

 

 4 2

Sample output

0011 

0101 

0110 

1001 

1010 

1100 

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    char a[25];
    int n,lenth,m,i;

    while(~scanf("%d",&n))
    {
      while(n--)
      {
          scanf("%d%d",&lenth,&m);
           memset(a,0,sizeof(a));//memset中对数组清零,该处的0相当于'\0',而不是'0'
      for(i=0;i<lenth-m;i++)
              a[i]='0';
           for(;i<lenth;i++)
            a[i]='1';
           printf("%s\n",a);
           while(next_permutation(a,a+strlen(a)))
                 {
                     printf("%s\n",a);
                 }
              if(n)
                printf("\n");
      }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值