Programmer's Algorithm Adventure Trip (i)---frog jumping step problem __ Algorithm

Source: Internet
Author: User
Frog jumping Step problem

Today in the cattle online to see some algorithm problems, I hard to force students one, but also learned the C,java, data structure, algorithm. To tell the truth, my data structure algorithm is not very good, so I will encounter a heap of problems. Today, I decided to write a blog about my own problem solving some ideas and ideas recorded. Problem Description

A frog can jump up to 1 steps at a time, or jump up to level 2. How many jumps are there in total for the frog to jump up the N-level steps?

The problem I looked at the feeling is not difficult, thinking for a while after a train of thought. Define a variable initialized to 0, use recursion to go through all the cases, the traversal of the test is not just skipped all the steps, if it is the variable plus 1. my way out.

public class Solution {//Self Test plus Main method public static void main (string[] args) {S
        Olution solution = new solution ();
        This ' 10 ' is just a test case for testing the answer right int n = solution.jumpfloor (10);
    System.out.print (n);
        //Start jump step, target is hop total step quantity public int jumpfloor (int target) {//variable int []n = new int[1] for stacking times
        A jump step, the next jump will be automatically called to jump step Jumponce (target,n);
    Returns the superposition number of variables return n[0]; The//left is the number of steps left, n is the variable public void jumponce (int left,int[] n) used for stacking times {//If the remaining steps are greater than 0, the next hop step operation is performed if (
            Left > 0) {jumponce (left-1,n);
        Jumponce (Left-2,n);
        //If the remaining steps are just 0, then the frog has skipped all the steps, the number of times the user superimposed the value of the variable plus one else if (left = 0) {n[0]++; }//Hidden condition--> If the number of steps left is negative, do nothing} 

Then this code executes on my computer without compiling problems and runs through. Tried a few test cases, found that there seems to be no problem, and then I copied the code to the cow online to run, and then was prompted to say that the code runs overtime, and then said that my code is too complex, inefficient. Alas, it seems to be true, it is well known that recursion is inefficient, and then my algorithm is almost entirely recursive to run ... So ... I have to find other solutions. I tried to change my recursion to a loop, but I did not modify it successfully.
Finally, I used the search engine to talk about this in a blog post, and then I mentioned that it was a question similar to the Fibonacci sequence. Oh, is that so? I tried using a loop to print out the answers to 10 of steps, and then I found out that it seemed to be true ....

1 2 3 5 8 13 21 34 55 89

I tidied up the idea again.
- ① Every time a frog can jump a step or two steps
- ② every time a frog jumps a step or two steps, the number of steps needed to jump and must have been counted before, plus the number of times previously counted .

So, after discovering this is a question of the Fibonacci sequence, the problem is simple. Soon I wrote the following code again. Higher -Efficiency solutions

public class Solution {public
    int jumpfloor (int target) {
        int answer =-1;

        if (target = = 1 | | target = 2) {
            answer = target;
        }
        else{
            int t1 = 1;
            int t2 = 2;
            int t3 =-1;

            for (int i=3;i<=target;i++) {
                t3 = t1 + T2;
                T1 = T2;
                t2 = t3;
            }

            answer = t3;
        }

        return answer;
    }

Reprint please declare the original article address http://blog.csdn.net/u014750748/article/details/50443093

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.