递归嘛,最简单,c++:
/*
* @
file : TestNBread.cpp
* @
author: slx
* @
date : 2018-04-27 12:21:26.865
* @
note : Generated by SlxTemplates
*/
#include <iostream>
using namespace std;
void dojob(int N, char buf[], int sum) {
if (N - sum == 1) {
cout << buf << 1 << endl;
} else if (N - sum == 2) {
cout << buf << 2 << endl;
cout << buf << 11 << endl;
} else {
int len = strlen(buf);
buf[len + 1] = 0;
buf[len] = '1';
dojob(N, buf, sum + 1);
buf[len + 1] = 0;
buf[len] = '2';
dojob(N, buf, sum + 2);
}
}
void dojob(int N) {
char *buf = (char *)malloc(N + 1);
memset(buf, 0, N + 1);
dojob(N, buf, 0);
free(buf);
}
int main(int argc, char *argv[])
{
dojob(7);
return 0;
}
结果:
111112
1111111
111121
11122
111211
11212
112111
11221
12112
121111
12121
1222
12211
21112
211111
21121
2122
21211
2212
22111
2221
请按任意键继续. . .