/设计一个程序,打印出1-200之间的斐波那契数列(说明:斐波那契数列指这样一个数列:1、1、2、3、5、8、13、21、34)
/
public class Demo {
public static void main(String[] args) {
fibo(200);
}
//计算下标数
static int countindex(int target) {
int count = 2;
int a = 1;
int b = 1;
int c = a + b;
while (c <= target){
a = b;
b = c;
c = a + b;
count++;
}
return count;
}
//打印斐波那契数列
static void fibo(int target){
int index = countindex(target);
int[] arr = new int[index];
for(int i=0;i<index;i++){
if(i == 0 || i == 1){
arr[i] = 1;}
else{
arr[i] = arr[i-1]+arr[i-2];
}
}
show(arr);
}
//遍历输出
static void show(int[] arr){
for (int i : arr) {
System.out.print(i+" ");
}
}
}