递归设计小白上楼梯

文章目录

题目

在这里插入图片描述

代码

import java.util.Scanner;

public class 递归设计小白上楼梯 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int res = f(n);
		System.out.println(res);

	}

	private static int f(int n) {
		if (n == 1) {
			return 1;
		}
		if (n == 2) {
			return 2;
		}
		if (n == 0) {
			// 相当于站在楼梯最顶端,被迫调试
			return 1;
		}
		return f(n - 1) + f(n - 2) + f(n - 3);

	}

}

发布了60 篇原创文章 · 获赞 4 · 访问量 1281

猜你喜欢

转载自blog.csdn.net/qq_43966129/article/details/105052055
今日推荐