Settings | Sign in | Sign up

There are currently 2 posts.

【试题】第39级台阶

Floor 1 巨大八爪鱼 3/18/16 21:21
#include <stdio.h>

int count = 0;
char str[100];

void search(int nStaircases, int nSteps = 0)
{
    if (nStaircases < 0) // 不能多走几阶梯
        return;
    if (nStaircases == 0) // 走完了所有阶梯时
    {
        if (nSteps % 2 == 0) // 走了刚好偶数步
        {
            str[nSteps] = '\0';
            //puts(str);
            count++; // 方案可行
        }
        return;
    }
    
    str[nSteps] = '1';
    search(nStaircases - 1, nSteps + 1); // 走一步
    str[nSteps] = '2';
    search(nStaircases - 2, nSteps + 1); // 走两步
}

int main(void)
{
    search(39);
    printf("%d\n", count);
    return 0;
}
Floor 2 巨大八爪鱼 3/18/16 21:23
这道题比较简单,关键是要读懂题。
其实就是要求计算偶数个1和2相加等于39的算式有多少个。

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
©2010-2025 Purasbar Ver3.0 [Mobile] [Desktop]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported license.