[001] leap_stage

Source: Internet
Author: User

[Description]There is a number in each stages that indicates the most stages you can leap up. now, giving an array which represents one kind of stair, please return if you can leap to the last stage, true if OK and vice versa.

E.g. stair [] = {, 0} return true;

Stair [] = {, 0} return false;

[Thought]Think about: what condition it wocould be true and false in current stair [0]? How to recursively call itself?

[Implementation]C code:

#include<stdio.h>int leapStage(int stair[], int size){        int i;        // if current stage number is zero, false, of course!        if(stair[0] == 0)        {                return 0;        }        // if current stage number indicate that we can jump over last stage, true.        else if(stair[0] >= size-1)        {                return 1;        }        // try each way while can‘t touch the last stages.        for(i = stair[0]; i>0; i--)        {                // if there is one way can do it, true.                if(leapStage(stair+i, size-i))                {                        return 1;                }        }        // after try all the way, can‘t get it.        return 0;}int main(){        int i;        int stair[]={2,1,2,0,3,1,0};        int size=sizeof(stair)/sizeof(stair[0]);        for(i=0; i<size; i++)        {                printf("%d, ",stair[i]);        }        if(leapStage(stair,size))        {                printf(" is true!\n");        }        else        {                printf(" is false!\n");        }}

 

[001] leap_stage

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.