這題寫了一個小時,我對遞迴似乎還不太熟...
#include <stdio.h>
#include <stdbool.h>
int n, h, d;
bool ans[16];
void go(int depth, int s)
{
int i;
if (s == d)
{
for (i = 0; i < depth; i++)
if (ans[i])
printf("1");
else
printf("0");
for (; i < n; i++)
printf("1");
printf("\n");
return;
}
ans[depth] = false;
go(depth+1, s+1);
if (n-depth > d-s)
{
ans[depth] = true;
go(depth+1, s);
}
}
int main(void)
{
int i, t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &h);
d = n - h;
go(0, 0);
if (t)
printf("\n");
}
return 0;
}
沒有留言:
張貼留言