zoukankan      html  css  js  c++  java
  • HDU 2563 统计问题(递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?

    pid=2563

    将向上移的步数设为a[n],将向左右移的步数设为b[n],有a[n]=a[n-1]+b[n-1],由于之前一步是向哪个方向,上移仅仅有向上一个方向;b[n]=a[n-1]*2+b[n-1],由于之前一步若向上移,则接下来就有左右两个方向都能够移动,若之前向左或右,则这一步仅仅能依照原来的方向移(原来的路已经坍陷)。

    得到走n步的方案有f[n]=a[n]+b[n],又由a[n]和b[n]的递推公式得到f[n]=f[n-1]*2+a[n-1],又b[n]=a[n]+a[n-1],终于推得f[n]=2*f[n-1]+f[n-2]。那么代码就非常easy了~

    #include<cstdio>
    #include<iostream>
    #include<sstream>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<climits>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    #include<map>
    using namespace std;
    int f[25];
    int main()
    {
        int c,n;
        scanf("%d",&c);
        while(c--)
        {
            f[1]=3;
            f[2]=7;
            for(int i=3; i<=20; i++)
                f[i]=2*f[i-1]+f[i-2];
            scanf("%d",&n);
            printf("%d
    ",f[n]);
        }
        return 0;
    }
    


  • 相关阅读:
    1.Hibernate配置
    CKEditor/FCKEditor的使用
    介绍一个好用的工具类库commons-beanutils
    SpringBean.xml配置
    MVC框架显示层——Velocity技术
    mysql CMD命令
    day 05
    day 04
    day03
    python day 02
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7103061.html
Copyright © 2011-2022 走看看