package work; public class task { // n 表示求数列中第 n 个位置上的数的值 public static int f(int n) { // 设置结束递归的限制条件 if (n == 1 || n == 2) { return 1; } // f(index) = f(index-1) + f(index-2) 递归公式 return f(n - 1) + f(n - 2); } public static void main(String[] args) { // 输出前 20 个数 for (int i = 1; i <= 20; i++) { System.out.print( f(i) + " "); } } }
Day16作业 斐波那契
最新推荐文章于 2024-06-12 11:59:04 发布