Je li mi može netko objasniti kako se ovaj program odvija:
Zadatak:
The frog wants to cross the river by jumping over n lotus leaves.She does that in jumps by two or three leaves forward or backward.
She cannot jump to the ground,which means she cannot go before the first leaf,neither after the last leaf.Jumping is finished when the frog is on the leaf n.
Write the recursive function that takes the number n, and returns the number of ways the frog can jump across the river in at most 17 steps.
Rješenje:
int frog(int n, int position, int step) {
if (step > 17)
return 0;
if (position > n || (step !=1 && position < 1))
return 0;
if (position == n){
return 1;
}
return
frog(n, position+2, step+1)
+ frog(n, position+3, step+1)
+ frog(n, position-2, step+1)
+ frog(n, position-3, step+1);
}
Hvala unaprijed.