AP协会第三周专题——递归

AP协会第三周——递归

把题集ak屁颠屁颠地跑来写笔记嘻嘻嘻
第一次学递归是在计算阶乘的时候:
4!答案是24
代码emmm……

#include<iostream>

using namespace std;

int Factorial(int n) {
    if (n == 1)
        return 1;
    else
        return n * Factorial(n - 1);
}

int main() {
    int n;
    cin >> n;
    cout << Factorial(n) << endl;
    return 0;
}

后来再加深认识就是斐波那契数列,求第n项

#include<iostream>

using namespace std;

int Fibonacci(int n) {
    if (n <= 2)
        return 1;
    else
        return Fibonacci(n - 1) + Fibonacci(n - 2);
}

int main() {
    int n;
    cin >> n;
    cout << Fibonacci(n) << endl;
}

这样我大致明白了,有些问题的结果是可以分成若干小问题分别求出来再经运算得出的,而这些问题大多数我们不能在短时间求得通项公式,但是我们可以通过理解它与众多小问题之间的关系求出递推公式,从而利用递归得出答案。

好,上正题(虽然在大佬眼里还是水题。。。。)

超级楼梯

有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?

输入

输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。

2
2
3

输出

对于每个测试实例,请输出不同走法的数量

1
2

很明显,要走上第M级阶梯,方法数等于走上第M-1级阶梯加上走上第M-2级阶梯之和,依次递推,我们得出递推公式:f(n) = f(n-2) + f(n-1)。

AC代码

#include<iostream>

using namespace std;

int back(int m) {
    if (m == 1 || m == 2)
        return 1;
    else
        return back(m - 1) + back(m - 2);
}

int main() {
    int n, m;
    cin >> n;
    while (n--) {
        cin >> m;
        cout << back(m) << endl;
    }
    return 0;
}

呀关键就是要看出递推的关系,我宿舍有个大佬直接深度搜索???,顺利地TLE了。。。
其实这个题就是斐波那契。
再来一个稍微难一点的。

Nine Interlinks

输入要求

The first line of the input contains an integer T (T <= 30), indicating the number of cases.

Each case consists of a simple integer n (1 < n < 30), which is the number of the total rings you need to get on the stick.

At the beginning, all rings are off the stick.

In each step, you can only get one ring on or off by the following rules:

  1. You can get the first ring on or off freely at each step.

  2. If the ith ring is on the stick, and the 1st, 2nd… (i-1)st rings are off the stick, you can get the (i+1)st ring on or off freely at each step.

输出要求

For each case, print in a single line the minimum number of steps you need to get n rings on the stick.

INPUT SAMPLE

4
2
3
4
5

OUT SAMPLE

2
5
10
21

HINT

The first sample: 1 on, 2 on.
The second sample: 1 on, 2 on, 1 off, 3 on, 1 on.

题目可以理解为有n个开关,全是off状态,第一个开关可以任意拨动,其他开关需满足条件时才可以任意拨动,求至少要拨动多少下才可以让开关们全部变为on状态。
条件:第i个开关 当且仅当 第i-1个开关为on且从第1到第i-2个开关为off的时候才可以任意拨动。

怎么样是不是有些难度了,姑且说一下我的见解,当要拨动第n个开关为on时,第n-1个开关一定是on的状态,也就是说结果必有一项加数是f(n-1),而f(n-1)只是把前n-1个开关都拨到on,为了拨动第n个开关我们还要把前n-2个开关再拨到off,这样结果就再加上f(n-2),当拨完第n个开关时,第n个开关与第n-1个开关是on状态,其他开关都是off,也就是要再把前n-2个再拨一遍,再加f(n-2),最后加上拨动第n个开关那一下,结果就是:
f(n) = f(n-1) + 2*f(n-2) +1
在这里插入图片描述

AC代码

#include <iostream>

using namespace std;

int dg(int m) {

    if (m <= 2)
        return m;
    else
        return 2 * dg(m - 2) + dg(m - 1) + 1;
}

int main() {
    int n, m;
    cin >> n;
    while (n--) {
        cin >> m;
        cout << dg(m) << endl;
    }
    return 0;
}

这个题我自己把我自己绕晕了。。。
没事还是搞出来了。
以后再补更难的吧,更难的做不来。。。。
以上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值