zoukankan      html  css  js  c++  java
  • 爬楼梯问题

          今天是20号呀,大喜过望,新的一学期,开工了。
         
          腾讯面试题:50个阶梯,你一次可以上一阶或两阶,走上去,共有多少种走法。

          f(n)=f(n-1)+f(n-2)
          f(n)定义:上n个阶梯,有f(n)种走法 
          要到 第 n 个阶梯,要么经过第 n-1 个阶梯,要么不经过
          
          斐波那契数,大数溢出问题没考虑。
    public class ClimbStairs{
        public static void main(String[] args)
        {
            long N = Integer.parseInt(args[0]); // steps of stairs
            long[] f = new  long[2]; // 存储 f(n-1) 和 f(n)
            f[0] = 1;
            f[1] = 1;
            // if ( N == 1) System.out.println( f[1] );
            int n = 1;
            while( N > n)
            {
                n++;
                long tmp = f[0];
                f[0] = f[1];
                f[1] = tmp + f[0];
            } // when n == N, break
            System.out.println( f[1] );
        }
    }


         

  • 相关阅读:
    单调栈
    单调队列
    线段树
    树状数组
    KMP模式匹配
    二分图最大匹配
    celery发送短信接口
    celery配置与基本使用
    celery介绍
    短信验证接口
  • 原文地址:https://www.cnblogs.com/learning-c/p/5202845.html
Copyright © 2011-2022 走看看