UVa 729 - The Hamming Distance Problem

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

由小到大,所以直接先擺定最右邊的 1 ,接著使用 std::next_permutation 取出下個組合。

Code

UVa 729
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<cstdio>
#include<algorithm>

int main()
{
char str[20];
int n, h, Case;
scanf("%d", &Case);
while (Case--)
{
scanf("%d%d", &n, &h);
int i;
for (i = 0; i < n - h; i++)
str[i] = '0';
for (i = 1; i <= h; i++)
str[n - i] = '1';

str[n] = NULL;
do
{
puts(str);
} while (std::next_permutation(str, str + n));
if (Case)
putchar('\n');
}

return 0;
}