39-Step problem

Source: Internet
Author: User

Title: Level 39th Step

Xiao Ming just finished watching the film "39th Step", when he left the cinema, he counted the number of steps in front of the auditorium, which happens to be level 39! Standing in front of the steps, he suddenly thought of a question:
If I can only take 1 or 2 steps per step. Take the left foot first, then turn left and right, and the last step is to take the right-hand foot, which means you have to go even step. So, how many different methods are there when you finish the 39 steps?

Please use the advantage of the computer to help xiaoming find the answer.

The required submission is an integer.

Program Analysis:

This program does not dwell on the problem of the left and right foot, from which the restriction condition is abstracted: The total number of steps is even;

We can pass the recursive implementation, the results of each recursive judgment, if the number of steps traversed is 39, then the recursive end, the number of steps to determine whether the walk is even, even if the above method calculator plus one, otherwise the invalid on the law.

Recursive algorithm:

Java

    public class Test003 {  
          
        private static int count;  
          
        public static void Main (string[] args) {  
            count = 0;  
            Walkup (0, 0);  
            System.out.println (count);  
        }  
          
        /** 
         * Analog up steps 
         *  
         * @param step to use, range 0-39 
         * @param TJ Total number of steps, Range 0-39/  
        private static void Walkup (int step, int tj) {  
            if (TJ > i) {return  
                ;  
            }  
              
            if (TJ = =) {  
                if (step%2 = 0) {  
                    count++  
                }  
                return;  
            }  
              
            Walkup (step+1, tj+1);  
            Walkup (step+1, tj+2);  
        }  
          
      

C++

    #include <iostream>  
    using namespace std;  
    int count=0;  
    void Fun (int stair,int step)  
    {   //stari is used to indicate the number of layers of the remaining staircase, and when equal to 0 o'clock stop recursion  
        //step is the number of steps that are traversed to determine whether an even, or not, match is required if  
         ( stair<0) return;   
         if (stair==0)   //39 staircase all walk out   
         {  
            if (step%2 = 0) count++;  
            return;  
         }   
         Fun (stair-1,step+1);   This step went up a stair   
         fun (stair-2,step+1);   This step has gone two steps   
    }  
    int main ()  
    {  
        fun (39,0);  
        cout<<count<<endl;  
        return 0;   
    }  

Non-recursive algorithm:

Package exercise;  
    	 /** * 39 Step Problem * @author Administrator */public class Main_exer {public static void Main (string[] args) { Dynamic DP idea//Create a two-dimensional array of 39*2 arrays S[i][j]//i represents the nth step, J says on the step for the left foot 0, the right foot 1//s[i][j] said in the level I step, just for J foot, the total may be
        Number of cases int[][] s = new int[39][2]; Defines the initial value, the array is from 0-38, representing 1-39 steps//below four initial values you don't understand, don't learn to program. s[0][0]=1;//First steps, the left foot of the number of cases only 1 s[0][1]=0;//first step,
        The number of the right foot is only 0 times, because the first diffuse must be left foot s[1][0]=1;//The second step, the case number of the left foot only 1 times s[1][1]=1;//The second step, the right foot of the number of cases only 1 times//Cycle calculation for (int i = 2;i<39;i++) {//s[i][0] The value of exactly is s[i-1][1] then diffuse left foot a step, just arrive s[i][0]//or s[i-2][1] situation again diffuse left foot two
            A step, just arrive s[i][0]//So s[i][0] value equals above two and s[i][0] = s[i-1][1] + s[i-2][1];
        Principle ditto s[i][1] = s[i-1][0] + s[i-2][0];  for (int i=0;i<s.length;i++) {for (int j=0;j<s[0].length;j++) {System.out.print (s[i][j) +
        	" "); } System.Out.println ();
 }
    }  
    
}
Reference: http://blog.csdn.net/bear_huangzhen/article/details/50068077

http://ask.csdn.net/questions/346001

http://blog.csdn.net/qsyzb/article/details/18991233

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.