A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, 3 steps at a time.

Source: Internet
Author: User

 

A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, 3 steps at a time. count how many possible ways the child can run up the stairs.

 

Class upstaircase {static void main (string [] ARGs) {/* enter your code here. read input from stdin. print Output to stdout * // string line1 = system. console. readline (). trim (); system. console. writeline (upstaircase. countwaysdp (2, new int [1, 100]); system. console. writeline (upstaircase. countwaysdp (3, new int [1, 100]); system. console. writeline (upstaircase. countwaysdp (4, new int [1, 100]); system. console. writeline (upstaircase. countwaysdp (5, new int [1, 100]); system. console. writeline (upstaircase. countwaysdp (36, new int [1, 100]); system. console. writeline (upstaircase. countwaysdp (38, new int [1, 100]); console. read ();} // o (3 ^ N ). exponential public static int countways (int n) {If (n <0) {return 0;} else if (n = 0) {return 1 ;} else {return countways (n-1) + countways (n-2) + countways (n-3); }}// DP: Dynamic programing. up to 37 will overflowed. using Long type is just delay but not resolve it. public static int countwaysdp (int n, int [] map) {If (n <0) {return 0;} else if (n = 0) {return 1 ;} else if (Map [N]> 0) {return map [N];} else {map [N] = countwaysdp (n-1, MAP) + countwaysdp (n-2, map) + countwaysdp (n-3, MAP); Return map [N] ;}}

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.