"title" A frog can jump up to 1 steps at a time, or jump to level 2. Ask the frog to jump on an n-level step with a total number of hops.
Ideas
* When N=1, there are 1 kinds of jumping method, f (1) = 1;
* When n=2, there are 2 kinds of jumping method, F (2) = 2;
* N=3, there are 3 kinds of jumping method, F (3) =3=f (3-1) +f (3-2) =f (2) +f (1);
* N=4, there are 5 kinds of jumping method, F (4) =f (4-1) +f (4-2) =f (3) +f (2) = 5;
"Code"
1 PackageCom.exe2.offer;2 3 /**4 * "title" A frog can jump up to 1 steps at a time, or jump to level 2. Ask the frog to jump on an n-level step with a total number of hops. 5 * "Ideas"6 * When N=1, there are 1 kinds of jumping method, F (1) =1;7 * When n=2, there are 2 kinds of jumping method, F (2) =2;8 * n=3, there are 3 kinds of jumping method, F (3) =3=f (3-1) +f (3-2) =f (2) +f (1);9 * n=4, there are 5 kinds of jumping method, F (4) =f (4-1) +f (4-2) =f (3) +f (2) =5;Ten * .... One * @authorWGS A * - */ - Public classQingwa { the Public intJumpstep (intN) { - int[] arrs=New int[]{0,1,2}; - intSum=0; - intone=2;//f (1) =1; + intTwo=1;//F (0) =0; - if(n<=2){ + returnArrs[n]; A}Else{ at for(inti=3;i<=n;i++){ -Sum=one+both ; -two=One ; -One=sum; - } - } in - returnsum; to + } - Public Static voidMain (string[] args) { theQingwa qw=NewQingwa (); * intN=qw.jumpstep (8); $System.out.println ("How many ways do frogs jump?") +n);Panax Notoginseng } -}
Sword Point offer series 8--frog jumping step problem