HDU-2040 super STAIR (simple recursion)

Source: Internet
Author: User

Super stair

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 10450 accepted submission (s): 5277

Problem description has a total of M-level stairs. At the beginning, you are at the first level. If you can only cross the upper level or Level 2 at a time, how many steps should you go to level M?

Input data first contains an integer N, indicating the number of test instances, followed by N rows of data, each row contains an integer m (1 <= m <= 40 ), the level of the stair.

Output for each test instance, please output the number of different steps

Sample Input

 
223

Sample output

 
12

CodeAs follows:

 
# Include <stdio. h> # include <stdlib. h> # include <math. h> # include <string. h> # include <time. h> int DP [45]; void Predeal () {/* memset (DP, 0, sizeof (DP); DP [0] = 1; for (INT I = 1; I <= 2; ++ I) for (Int J = I; j <= 40; ++ J) {DP [J] + = DP [J-I]; // printf ("DP [% d] = % d \ n", J, DP [J]);} */dp [1] = 1, DP [2] = 2; for (INT I = 3; I <= 40; ++ I) DP [I] = DP [I-1] + dp [I-2];} int main () {int t; Predeal (); scanf ("% d", & T ); while (t --) {int ask; scanf ("% d", & ask); printf ("% d \ n", DP [ask-1]);} return 0 ;}


during this period of time, because there were too many people in touch with backpacks, I first thought about the optimization of the motherboard function when I came back to this question, so I was confused by the mistake, there was no difference in coins in the original primary function, and 1 and 2 in this case were sorted in order. Xiaochen (a senior) came over and looked at it (with a smile). Isn't that a simple recursive question? F [I] = f [I-1] + F [I-2]; because each step can be one or two steps above.

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.