2012年8月24日

UVa 729 - The Hamming Distance Problem


Practice to use next_permutation!
#include <stdio.h>
#include <algorithm>

using std::next_permutation;

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int i, n, h;
    scanf("%d%d", &n, &h);
    int *num = new int[n];
    for (i = 0; i < n - h; i++)
      num[i] = 0;
    for (; i < n; i++)
      num[i] = 1;

    do {
      for (i = 0; i < n; i++)
        printf("%d", num[i]);
      puts("");
    } while (next_permutation(num, num + n));
    if (t) puts("");
    delete[] num;
  }
  return 0;
}

沒有留言:

張貼留言