/* * sequences of 1s and 0s with exactly 1 pair of consecutive 1s. */ #include #include #include struct foo { char str [100]; int num; } bs [800]; int N, num_bs; void sort (void); void gen (char *s); int ok (char *s); int main (void) { char s [100]; int i; while (gets (s) != NULL) { sscanf (s, "%d", &N); s [0] = '\0'; num_bs = 0; gen (s); sort (); for (i = 0; i < num_bs; i++) { printf ("%s\n", bs [i].str); } } return 0; } void sort (void) { struct foo tmp; int i, j; for (i = 0; i < num_bs; i++) { for (j = i + 1; j < num_bs; j++) { if (bs [i].num > bs [j].num) { tmp = bs [i]; bs [i] = bs [j]; bs [j] = tmp; } } } } void gen (char *s) { int len; if (strlen (s) == N) { if (ok (s)) { strcpy (bs [num_bs].str, s); bs [num_bs].num = strtol ( bs [num_bs].str, (char **)NULL, 2); num_bs++; } return; } len = strlen (s); s [len + 1] = '\0'; s [len] = '0'; gen (s); s [len] = '1'; gen (s); s [len] = '\0'; } int ok (char *s) { int i, p, c; p = 0; c = 0; for (i = 0; i < strlen (s); i++) { if (s [i] == '0') { if (c == 2) p++; else if (c > 2) return 0; c = 0; } else { c++; } } if (p == 1 && c < 2) return 1; if (p == 0 && c == 2) return 1; return 0; }