发布网友 发布时间:2022-05-02 08:40
共2个回答
热心网友 时间:2023-10-14 06:40
运用递归来写fibonacci的算法,其中当输入0时,返回0,当输入1时,返回1
public static void main(String args[]){最后的效果如下
热心网友 时间:2023-10-14 06:41
用java写用递归算法计算,并输出Fibonacci数列的前20项。
public class Fib_array {,public static void main(String[] args) {, int fib[] = new int[20];int i, n = 20;fib[0] = 0;fib[1] = 1 for (i = 2; i < fib.length; i++) fib[i] = fib[i - 1]+ fib[i - 2];for (i = 0; i < 5; i++)System.out.print(" " + fib[i]);System.out.println();
for (i = 5; i < 10; i++)System.out.print(" " + fib[i]); System.out.println(); for (i = 10; i < 15; i++)System.out.print(" " + fib[i]);.out.println(); for (i = 15; i < 20; i++)System.out.print(" " + fib[i]);System.out.println();