- #include<iostream>
- using namespace std;
- int a[100];
- void louti(int index,int n){
- // 递归边界: 满足条件则输出行走步数
- if(n==0){
- for(int i=0;i<index;i++){
- cout <<a[i] << " ";
- }
- cout << endl;
- return;
- }
- // 走 1 步的情况
- if(n>=1){
- a[index]=1;
- index++;
- louti(index,n-1);
- index--;
- }
- // 走 2 步的情况
- if(n>=2){
- a[index]=2;
- index++;
- louti(index,n-2);
- index--;
- }
- // 走 3 步的情况
- if(n>=3){
- a[index++]=3;
- louti(index,n-3);
- index--;
- }
- }
- int main(){
- int n;
- cin>> n;
- louti(0,n);
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2930662.html