用java編寫程序輸出Fibonacci序列?
用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();