package com.itheima.level02;
/*
* 有一分数序列:
* 2/1,3/2,5/3,8/5,13/8,21/13...
* 定义一个方法求出这个数列的前20 项之和。
*/
public class Test05 {
public static void main(String[] args) {
int[] arr = new int[40];
arr[0] = 2;
arr[1] = 1;
arr[2] = 3;
arr[3] = 2;
double sum = (double)(arr[0])/arr[1] + (double)(arr[2])/arr[3];
for(int i=4; i<arr.length; i++){
arr[i] = arr[i-2] + arr[i-4];
if(i%2 != 0){
sum += (double)(arr[i-1])/arr[i];
}
}
System.out.println(sum);
}
} |