#include <iostream> #include <stdlib.h> #include <math.h> using namespace std; class Solution { public: int climbStairs(int n) { double estim = 0.4472135955 * pow(1.618033988745, n+1); return ( abs(ceil(estim)-estim) < abs(estim-floor(estim)) )? ceil(estim) : floor(estim); } }; int main() { int n = 7; Solution s; for (int i=0; i <= n; i++) cout << "climbStairs(" << i << "): " << s.climbStairs(i) << endl; return 0; }
|