题目:
猴子登山:有N个台阶,0<N<=50, 猴子每次只跳1个或者3个台阶,输入N, 输出猴子有多少种方式到达台阶终点。
递归求解:
public static void test(){ Scanner scanner=new Scanner(System.in); int n=Integer.parseInt(scanner.nextLine()); scanner.close(); int num=count(0,n,n,0); System.out.println(num); } public static int count(int hasFinish,int noFinish,int n,int count){ int countX=count; int hasFinishN=hasFinish; int noFinishN=noFinish; if (hasFinishN==n){ return countX+1; } if (hasFinishN<n){ hasFinishN=hasFinishN+1; noFinishN=noFinishN-1; countX=count(hasFinishN,noFinishN,n,countX); } if (noFinish+1>=3){ hasFinishN=hasFinishN-1+3; noFinishN=noFinishN+1-3; countX=count(hasFinishN,noFinishN,n,countX); } return countX; }