39 Steps (race title, go language Implementation)

Source: Internet
Author: User
Tags ming
This is a creation in Article, where the information may have evolved or changed.

Problem Recurrence:

Xiao Ming just finished the film "39th Steps", when he left the cinema, he counted the number of steps before the auditorium, which happens to be level 39!

Standing before the steps, he suddenly thought of a question:

If I can only go 1 or 2 steps at a step. Take the left foot first, then alternately, the last step is to go to the right foot, that is, a total of even steps. So, how many different ways to go on the 39-step level?

Please take advantage of the computer to help Xiao Ming find the answer.

Recursive solution (Go language implementation)

Package Mainimport ("FMT")/* Recursive core  Author: Days of *@i indicates the number of stair climbs * @now represents the current floor * @top represents the total number of stairs *@*count Count */func F (i, now, top int , Count *int) {if now = = Top {//Last must be right foot, 0 left foot, 1 foot right//end when I add 1, so judgment and 0 compare if (i&1) = = 0 {(*count) + +}} else if now < top { F (i+1, now+1, Top, Count) F (i+1, now+2, Top, Count)}}func main () {var count int = 0var Top int = 39//Get input total progression fmt from keyboard. SCANF ("%d", &top) F (0, 0, top, &count) fmt. PRINTLN (count)//Let the screen stay in FMT. SCANF ("%c", &top) fmt. SCANF ("%c", &top)}

Related Article

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.