You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
947 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int n;
int ways;
/**
* @param step
* @param level
* N
* : 5 8
* : 15 987
* +
*/
void dfs(int level, int step) {
if (level == n) {
ways++;
//输出结果
for (int i = 0; i < step; i++)printf("%d\t", a[i]);
printf("\n");
}
//2种分枝
for (int i = 1; i <= 2; i++) {
//可行
if (level + i <= n) {
a[step] = i;//记录解向量
dfs(level + i, step + 1);
}
}
}
int main() {
cin >> n;
dfs(0, 0);
printf("一共 %d 种方法。\n", ways);
return 0;
}