Monkey pick a banana can pick 1 or 2, a total of 50, ask how many kinds of pick-up method?

Source: Internet
Author: User

The problem comes from knowing
When I finished the discovery, the knowledge is better than I write, I am crying!!!
When I'm done, I find out that it's not what it is. The function, when affixed to the LUA code n is very large, the estimate is not resistant, the code needs to be optimized.
function banana (n)    local c = 1;    if n = = 0 Then        return 0    ElseIf n = 1 then        return 1    elseif n = 2 then        return 2    end    return (BA Nana (n-1) + banana (n-2)) * C;endfor i = 1,  ten do    print (banana (i)) end


Optimized code:
Local know = {}function banana (n)    if know[n] then        return know[n]    end    local c = 1;    if n = = 0 Then        know[n] = 0        return 0    elseif n = = 1 then        know[n] = 1        return 1    elseif n = 2 Then
   know[n] = 2        return 2    end    Local r = (banana (n-1) + banana (n-2)) * C;    Know[n] = R    return rendfor i = 1, do    print (I, banana (i)) end



Monkey pick a banana can pick 1 or 2, a total of 50, ask how many kinds of pick-up method?

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.