PHP step jumping Problem

Source: Internet
Author: User
1 <? PHP 2 # Step jumping Problem 3 # There are two methods to skip a step: one step or two steps, there are several methods to skip n steps. 4 # If n = 1, there is only one method. n = 2 There are two ways to skip level 1 at a time, skip Level 2 5 at a time # If n> 2, there are two possible situations. The first is the first hop level 1, and the remaining hop method is F (n-1) 6 # The second is the first hop Level 2, so the rest of the hop method is F (n-2), then f (n) = f (n-1) + f (n-2) 7 # so there is a recursive formula 8 # | 1 n = 1 9 # F (n) = <2 n = 210 # | f (n-1) + f (n-2) n> 211 # then the solution to this problem is similar to fabonacci's 12 13 function jump ($ n) {14 if ($ n = 1) return 1; 15 if ($ n = 2) return 2; 16 $ A = 1; 17 $ B = 2 ; 18 $ I = 3; 19 while ($ I <= $ n) {20 $ c = $ A + $ B; 21 $ A = $ B; 22 $ B = $ C; 23 $ I ++; 24} 25 return $ C; 26} 27 28 echo jump (6); 29?>

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.