Title: A frog can jump up to 1 steps at a time, or jump up to level 2. Ask the frog to jump on an n-level step with a total number of hops.
My train of thought: At first my idea was to think of this as a mathematical problem, n=i*1+k*2 all the I and k that might satisfy the formula. Then in pairs I and k do permutation combinations. It is clear that the range of I should be 0<i<=n, so we have started the iteration. Put the code on it below. Write down all the notes!
public int Jumpfloor (int target) {int step = 0;for (int i = 0; I <= target; i++) {if (0 = = (target-i)% 2) {//Just satisfy this The condition is that the combination of conformity//As long as it is possible to meet the situation//I represents the number of hops, if i=0 or i=target this can not appear in multiple permutations if (i = = 0 | | i = = target) {step++;//Only one arrangement, or all 1 , or all 2} else {/* * appear in full alignment. Using the idea of permutation combination to figure out how many ways to go this time */int onenum = i;//for 1 has i int twonums = (target-i)/2;//for 2 There are so many/** * all permutations Aii/akk*ajj (i means 1 and 2 of the CPC) Number, K is the number of 1, * J indicates the number of 2, Simplecircle is a number of factorial) */int Stepcount = simplecircle (Onenum + twonums)/(Simplecircle (onenum) * SIM Plecircle (twonums)); step + = Stepcount;}}} return step;} public int simplecircle (int num) {///simple loop computed factorial int sum = 1;if (num < 0) {//Determine if incoming number is negative throw new Illegalargumentexceptio N ("must be a positive integer!"); /throw unreasonable parameter exception}for (int i = 1; I <= num; i++) {//loop numsum *= i;//multiply once per loop}return sum;//returns the value of factorial}
Summary: The author of this solution although the sense of thought is clearer, but the feeling is not very clever! (In fact, the figure you will find is actually a Fibo series) no bright spots, but this solution when 2 becomes the other number of the time is universal
Let's take a look at the solution of the Internet (this is a sword refers to the above topic), the first is to combine the Fibonacci series to solve the problem
Analysis: 1) when n = 1, there are only 1 hops; when n = 2 o'clock, there are two kinds of jumps; when n = 3 o'clock, there are 3 kinds of jumps; when n = 4 o'clock, there are 5 kinds of jumps; when n = 5 o'clock, there are 8 kinds of jumping method;
The law is similar to the Fibonacci sequence
2)
If n=1, the total number of steps f (n) = 1; If n=2, the total number of steps f (n) = 2.
On the other hand, when n>=3, the remaining number of steps f (n), if the next step, then the remaining number of steps is f (n-1), if the next jump two steps, then the remaining number of steps is f (n-2), so:f (n) =f (n-1) +f (n-2).
int Fib (int n) {//typical Fibo sequence method if (n <= 0) {cout << "error!" << endl;return-1;} if (1 = = N) {return 1;} else if (2 = = N) {return 2;} Else{return fib (n-1) + fib (n-2);}}
-----------------------------------------------Split Line-------------------------------------------------------------------------------
The following is the problem of the change, very good!
Q: A frog can jump up to 1 steps at a time, or jump up to level 2 ... It can also jump on the N-level, at which point the frog jumps up the level of the N-step total number of hops?
Using FIB (n) to indicate that the frog jumps on the N-Step steps, the frog jumps on the N-Step step number 1 (n-Step) and sets the FIB (0) = 1;
When n = 1 o'clock, there is only one method of jumping, that is, the 1-Step Jump: Fib (1) = 1;
When n = 2 o'clock, there are two ways of jumping, one-step and second-order jumps: fib (2) = FIB (1) + fib (0) = 2;
When n = 3 o'clock, there are three ways to jump, the first step out of one, followed by FIB (3-1) in the Jump method, the first second out of the second, there is a fib (3-2) in the Jump method, after the first jump out of third order, followed by FIB (3-3) in the Jump method
FIB (3) = FIB (2) + fib (1) +fib (0) = 4;
When n = n, there is a total of n jumps, the first step out of one order, followed by FIB (n-1) in the Jump method, after the first second out of the second, there is a fib (n-2) in the Jump method ..... ..... After the first step out of the N-order, there is a Fib (n-n) jump in the back.
FIB (n) = fib (n-1) +fib (n-2) +fib (n-3) +..........+fib (n-n) =fib (0) +fib (1) +fib (2) +.......+fib (n-1)
And because Fib (n-1) =fib (0) +fib (1) +fib (2) +.......+fib (n-2)
Two-type subtraction: FIB (n)-fib (n-1) =fib (n-1) ===== "fib (n) = 2*fib (n-1) n >= 2
The recursive equation is as follows:
】
The code is very simple, but the analysis is not so simple:
public int JUMPN (int n) {if (n = = 0 | | n = = 1) {return 1;} Return 2 * JUMPN (n-1);}
Other ideas have not been thought out yet, put them first.
Reprint Please specify http://blog.csdn.net/a837199685
Frog Jumping Step problem