package com.main.xxx;publicclassTestStep{publicstaticvoidmain(String[] args){
TestStep testStep =newTestStep();//获取当前系统时间long start = System.currentTimeMillis();
System.out.println(testStep.f(40));//165580141long end = System.currentTimeMillis();
System.out.println(end-start);//359}//实现f(n):求n步台阶,一共有几种走法publicintf(int n){if(n<1){thrownewIllegalArgumentException(n +"甭能小于一");}if(n ==1|| n ==2){return n;}returnf(n-2)+f(n-1);}}
package com.main.xxx;publicclassTestStep2{publicstaticvoidmain(String[] args){
TestStep2 testStep2 =newTestStep2();long start = System.currentTimeMillis();
System.out.println(testStep2.loop(40));//165580141long end = System.currentTimeMillis();
System.out.println(end - start);//<1ms}publicintloop(int n){if(n<1){thrownewIllegalArgumentException(n +"甭能小于一");}if(n ==1|| n ==2){return n;}int one =2;//初始化为走到第二季台阶的走法int two =1;//初始化为走到第一级台阶的走法int sum =0;for(int i =3; i <= n; i++){//最后跨2步 + 最后跨1步的走法
sum = two + one;
two = one;
one = sum;}return sum;}}