爬楼梯问题

      今天是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] );
    }
}


     

原文地址:https://www.cnblogs.com/learning-c/p/5202845.html