package shuzu;
// Fibonacci 数列为{0,1,1,2,3,5,8,13,21,34,55,……},其首两项为0和1,以后各项时其前两项之和。
import java.util.Arrays;
import java.util.Scanner;
public class ArrayDemo1 {
public static void main(String[] args) {
//指定数组的长度
Scanner in=new Scanner(System.in);
System.out.println("请输入数组的长度: ");
int n=in.nextInt();
//声明一个数组,存放数列
int fib[] = new int[n];
fib[0]=0;
fib[1]=1;
//遍历数组,为第三个元素后的每个元素赋值
for (int i = 2; i < fib.length; i++) {
fib[i] = fib[i-1]+fib[i-2];
}
//输出数组
for (int i = 0; i < fib.length; i++) {
System.out.print(fib[i]+" ");
}
}
}
标签:fib,数列,int,System,斐波,数组,那契,Scanner
From: https://www.cnblogs.com/knlf/p/16785706.html