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.
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 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.
1 4 2
0011 0101 0110 1001 1010 1100
还是全排列
#include <stdio.h> #include <string.h> char s[100],ch; void sort(int x,int y) {int i,j; for (i=x;i<y;i++) for (j=i+1;j<=y;j++) if (s[i]>s[j]) {ch=s[i];s[i]=s[j];s[j]=ch;} ;} int main() {int h,t,l,i,j,pos,min; scanf("%d\n",&t); while (t--) {scanf("%d%d",&l,&h); for (i=0;i<l;i++) if (i>=l-h) s[i]='1';else s[i]='0'; s[l]='\0'; //开始没加这句话,导致前面一组数N》后面的使后面的排列多输出后缀0 puts(s); while (l) {pos=l-1; while ((pos>0)&&(s[pos]<=s[pos-1])) --pos; if (pos) {min=pos; for (i=pos+1;i<l;i++) if ((s[i]<s[min])&&(s[i]>s[pos-1])) min=i; ch=s[pos-1];s[pos-1]=s[min];s[min]=ch; sort(pos,l-1); puts(s); } else break; } if (t) printf("\n"); } return 0; }