最近遇到一道面试题50阶台阶,上5阶,下3阶,几次能上去,个人百度了一下没找到就自己写了一个仅供参考
水平有限写的不好,期望大佬指教
public class Test {
public static void main(String[] args){
int i = 50;
int a = 0;
int s = 5;
int x = 3;
int count = stepByOne(a, i, s, x);
System.out.println(count );
}
/**
*
* @param count 总次数
* @param tjCount 台阶总数
* @param s 上的台阶数
* @param x 下的台阶数
* @return
*/
public static int stepByOne(int count ,int tjCount , int s ,int x){
if(tjCount - s <= 0 ){
count++;
return count;
}else {
count ++ ;
tjCount = (tjCount-s)+x;
return stepByOne(count,tjCount,s,x);
}
}
}