Sword refers to the question of jumping steps for frogs

Source: Internet
Author: User

(1) A frog can jump up to 1 steps at a time, or jump up to 2 levels. Ask the frog to jump on an n-level step with a total number of hops. Recursive mode
   
  
  1. public static int f(int n) {
  2. //参数合法性验证
  3. if (n < 1) {
  4. System.out.println("参数必须大于1!");
  5. System.exit(-1);
  6. }
  7. if ( n == 1 Span class= "pun" >| | n == 2 ) return 1 ;
  8. else return f(n - 1) + f(n - 2);
  9. }


Non-recursive mode
  
 
  1. public static int fx(int n) {
  2. //参数合法性验证
  3. if (n < 1) {
  4. System.out.println("参数必须大于1!");
  5. System.exit(-1);
  6. }
  7. //n为1或2时候直接返回值
  8. if (n< 2) return 1;
  9. //n>2时候循环求值
  10. int res = 0;
  11. int a = 1;
  12. int b = 1;
  13. for (int i = 2; i <= n; i++) {
  14. res = a + b;
  15. a = b;
  16. b = res;
  17. }
  18. return res;
  19. }

(2) A frog can jump up to 1 steps at a time, can also jump on 2 levels ... 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?

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:

  
 
  1. if(number<2)return 1;
  2. //n>2时候循环求值
  3. int res = 0;
  4. int a = 1;
  5. for (int i = 2; i <= number; i++) {
  6. res = 2*a;
  7. a= res;
  8. }
  9. return res;



From for notes (Wiz)

Sword refers to the question of jumping steps for frogs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.