Recursion-down stairs

Source: Internet
Author: User

Total time limit: 1000ms memory limit: 1000kB
Describe
from upstairs to downstairs a total of h steps, each step there are 3 ways to go: Walk 1 steps, walk 2 steps, walk 3 steps. Q. How many options can you get out of and print out a specific plan?
Input
number of steps H
Output
various ways to go and the total number of solutions
Sample input
5
Sample output
Plan 1:32
Plan 2:311
Plan 3:23
Plan 4:221
Plan 5:212
Plan 6:2,111
Plan 7:131
Plan 8:122
Plan 9:1211
Plan 10:113
Plan 11:1121
Plan 12:1112
Plan 13:11,111
Number of Plans:13
Thought: Because every step of the way is slightly the same, you can use a recursive method.
Code:
1#include <iostream>2 using namespacestd;3 4 inttake[ About];5 intnum =0;//Number of programmes6 voidTry (intIints)7 {8      for(intj=3; J>0; j--)9     {Ten         if(i>=j) One         { ATake[s] =J; -             if(i==j) -             { thenum++; -cout<<"Plan"<<num<<":"; -                 //output sub-scheme -                  for(intk=1; k<=s; k++) +cout<<Take[k]; -cout<<Endl; +             } A             Else //not yet to go downstairs atTry (I-j, s+1); -         } -  -     } - } -  in intMain () - { to     intN; +Cin>>N; -  theTry (N,1); *  $cout<<"Number of plans:"<<num<<Endl;Panax Notoginseng     return 0; -}

Recursion-down stairs

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.