0-1字符串
对于长度为N位(N<32)的一格字符串,每一位都可能是0或1,请从小到大输出者2^N种01字符串
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int n; scanf("%d",&n); char *t = (char *)malloc(sizeof(char)*(n+1)); memset(t,‘0‘,n); t[n] = ‘\0‘; for(int i=0;i<(1 << n);i++)//2^n次 { puts(t); t[n-1] += 1; if(t[n-1] > ‘1‘) { for(int j=2;j<=n;j++) { t[n-j] += 1; t[n-j+1] =‘0‘; if(t[n-j] == ‘1‘) break; } } } return 0; }
原文:http://www.cnblogs.com/520xiuge/p/5223638.html