Skip to content
On this page

题目描述

读取一个数字n,在一个递归函数内输出下面的样式

1 2 3 4 5 6 7 8 .... n
n n-1 n-2 n-3 ..... 1
1
2

例如输入10,输出

1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
1
2

解析

仔细体会下面的代码

代码

c
#include <cstdio>

int n;
void dfs(int dep){
    if( dep == n+1){
        printf("\n");
        return;
    }
    printf("%d ",dep);
    dfs(dep+1);
    printf("%d ",dep);
}
int main(){
    scanf("%d",&n);//输入数字
    dfs(1);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17