zoukankan      html  css  js  c++  java
  • HDOJ_ACM_超级楼梯

    Problem Description
    有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
     
    Input
    输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
     
    Output
    对于每个测试实例,请输出不同走法的数量
     
    Sample Input
    2
    2
    3
     
    Sample Output
    1
    2

    First--------------------accepted

    View Code
     1 #include <stdio.h>
     2 int main()
     3 {
     4     int n, m, count, i, j;
     5     int a[45] = {0};
     6     a[1] = 1;
     7     for (i = 1; i <= 40; i++)
     8     {
     9         a[i + 1] += a[i];
    10         a[i + 2] += a[i];    
    11     }
    12     scanf("%d", &n);
    13     while (n--)
    14     {
    15         scanf("%d", &m);
    16         printf("%d\n", a[m]);
    17     }
    18     return 0;
    19 }

    f(n): the number of ways

    f(n + 1) = f(n + 1) + f(n)

    f(n + 2) = f(n + 2) + f(n)

    it means that the number of ways in n+1 is the number in n plus the own number. The other means the number of ways in n+2 is the number in n plus the own number.

     Second---------------accepted

    View Code
     1 #include <stdio.h>
     2 int main()
     3 {
     4     int n, m, count, i, j;
     5     int a[45] = {0};
     6     a[1] = 1;
     7     a[2] = 1;
     8     for (i = 3; i <= 40; i++)
     9     {
    10         a[i] = a[i - 1] + a[i - 2];
    11     }
    12     scanf("%d", &n);
    13     while (n--)
    14     {
    15         scanf("%d", &m);
    16         printf("%d\n", a[m]);
    17     }
    18     return 0;
    19 }

    Maybe it will be more easy to understand.

    f(n)=f(n-2)+f(n-1)

    Third-------------------Time Limit Exceeded

    View Code
    Because the recursion is waste of time, then it's wrong, but the result is right and simplier.
     
     
     
     
  • 相关阅读:
    水煮栗子
    张至顺道长羽化登仙+说修行(道经每日清修)
    治疗口腔溃疡的穴位按摩方法
    一年四季的时令蔬菜水果表
    坐式养生八段锦口诀及练法图解
    SOA建设规划
    生鲜电商业务流程规划
    产品定义到产品推广的思路
    生鲜财务核算
    税率与存货、供应商关系
  • 原文地址:https://www.cnblogs.com/chuanlong/p/2759811.html
Copyright © 2011-2022 走看看