递归:熊孩子123步走楼梯
/*
熊孩子上楼梯,可以迈1步、2步或者3步,共有n个台阶(1<=n<=30),
共有多少种走法?
输入:4
输出:
1 1 1 1
1 1 2
1 2 1
1 3
2 1 1
2 2
3 1
*/
#include<iostream>
using namespace std;
int a[1000];
int index=0;
void out(){
for(int i=0; i<index; i++){
cout << a[i] << " ";
}
cout << endl;
return;
}
void tai_jie(int n){
if (n == 0) {
out();
}
// 迈1步
if (n >= 1) {
a[index++] = 1;
tai_jie(n-1);
index--;
}
// 迈2步
if (n >= 2) {
a[index++] = 2;
tai_jie(n-2);
index--;
}
// 迈3步
if (n >= 3) {
a[index++] = 3;
tai_jie(n-3);
index--;
}
}
int main(){
int m;
cin>>m;
tai_jie(m);
return 0;
}