写了一个,仅供参考。没什么注释,写的也比较烂,就当抛砖引玉吧~
程序代码:
#include <stdio.h> int nest(int n) { int i = 0; for (; (1 << i) <= n; ++i); return i; } int sets[64]; int set(int n, int layer) { static i = 0; if (!i) { sets[i] = layer; for (++i; i <= layer; ++i) sets[i] = n >> (i-1); return --i; } while (i>1 && --sets[i] == 0) --i; if (i == 1) return i = 0; for (; i < layer; ++i) { sets[i+1] = sets[i] >> 1; if (sets[i+1] == 0) return i = 0; } return 1; } void putset() { int i = sets[0]; for (; i > 0; --i) printf("%d", sets[i]); printf(", "); } int main() { int n, k; scanf("%d", &n); k = nest(n); int i = 1; for (; i <= k; ++i) { while (set(n, i)) putset(); } putchar('\n'); return 0; }