public int JumpFloor(int target) { if(target==0)
return 1;
if(target==1)
return 1;
else{
return JumpFloor(target-1)+JumpFloor(target-2);
}
} #include <iostream>
using namespace std;
int Fibonacci( int n)
{
int *F= new int [n];
F[ 0 ]= 0 ;
F[ 1 ]= 1 ;
for ( int i= 2 ;i<=n;++i)
F[i]=F[i- 1 ]+F[i- 2 ];
return F[n];
}
int main(){
int num;
std::cin >> num;
std::cout << Fibonacci(num);
return 1 ;
}
其实这道题,就是在考察斐波那契数列的问题,但是递归下的斐波那契数列可能存在有溢出等问题,所以针对斐波那契数列来说的,非递归的写法要优于递归的写法,下面给出递归和非递归的两种写法:
递归:
#include<stdio.h>
int fib(int n)
{
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int main()
{
int n = 0;
int ret = 0;
(void)scanf("%d", &n);
ret = fib(n);
printf("%d", ret);
return 0;
} 非递归:
#include<stdio.h>
int main()
{
int a = 1;
int b = 1;
int c = 1;
int n = 0;
(void)scanf("%d", &n);
while (n >= 3)
{
c = a + b;
a = b;
b = c;
n--;
}
printf("%d", c);
return 0;
}
function getC($n,$start=1){
$num = 1;
for(;$start<=$n;$start++){
$num *= $start;
}
return $num;
}
function getJump($x,$y){
//echo $x.":".$y;
if($x<=0 || $y <= 0){
if($x <= 0 && $y <= 0){
return 0;
}
return 1;
}
$n = $x +$y;
$num = getC($n,$n-$y+1)/getC($y);
return $num;
}
function jumpType($n){
$count = 0;
for($x=0;$x<=$n;){
if(($n - $x)%2 == 0){
$y = ($n - $x)/2;
$num = getJump($x,$y);
//echo "=".$num."<br/>";
$count += $num;
$x = $x + 2;
}else{
$x = $x + 1;
}
}
return $count;
}
echo jumpType(11);