C语言函数实现《青蛙跳台阶》问题
青蛙跳台阶是一类典型题: 是指青蛙跳跳台阶可以一次只跳1个台阶,也可以跳2个台阶;;所以当1个台阶,有1种跳法;跳2个台阶有2种跳法;跳3个台阶就会有3种跳法;4个台阶就会有5种跳法;5个台阶有8种跳法;6个台阶就会有13种跳法…
代码实现:
// hanoi.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "stdafx.h"
int Jump(int n)
{
if(n<3)
{
return n;
}
else
{
return Jump(n-2)+Jump(n-1);
}
}
int main(int argc, char* argv[])
{
int n = 1;
int res = 0; //青蛙跳n台阶的方法数
printf("请输入青蛙跳的台阶数:》");
scanf("%d",&n);
res = Jump(n); //调用函数
printf("方式共有:%d种\n",res);
return 0;
}
可以通过运行结果进行验证: