这会儿满足要求了吧?处理了a==1的特殊情况,并且没有用到字符串,数组和递归(要求真TMD多……)
程序代码:
#include <stdio.h> #include <stdlib.h> #define A 0xAAAAAAAA void bval(int n, size_t wide) { int i, res = 0; for (i = wide; i--; n >>= 1) res = (res << 1) | (n & 1); for (i = wide; i--; res >>= 1) putchar('0' + (res & 1)); } int main(void) { int a, b; while (scanf("%d%d", &a, &b) == 2 && a > 0 && a <= b && b >= 1 && b <= 31) { int n = 0, orig_n, bn = 1, orig_bn; if (a == 1) { bn = 1; for (n = 1; n < b; ++n) bn = (bn << 1) | 1; for (n = 0; n <= bn; ++n) { bval(n, b); putchar('\n'); } continue; } while (n < b - a + 1) { int c = 0, cc = b - n - a; orig_n = n; orig_bn = bn; while (n > 0) { int d; for (d = 0; d < bn; ++d) { bval(c, cc); bval(A >> (32 - a), a); bval(d, n); putchar('\n'); } n -= 1; bn >>= 1; c = (c << 1) | 1; cc += 1; } bval(c, cc); bval(A >> (32 - a), a); putchar('\n'); n = orig_n + 1; bn = orig_bn << 1; } } return 0; }