王道oj/problem13(用递归数楼梯)

网址:http://oj.lgwenda.com/problem/13

思路:用递归写step(int n):return step(n-1)+step(n-2);

          停止条件是:n=1为1;n=2为2.

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//走楼梯
int step(int n)
{
if (1 == n)return 1;
if (2 == n)return 2;
return step(n - 1) + step(n - 2);
}
int main()
{
int n;
int ret;
scanf("%d", &n);
ret = step(n);
printf("%d\n", ret);
return 0;
}

posted @ 2022-04-08 19:31  zccccc!  阅读(64)  评论(0)    收藏  举报