题目描述
有一分数序列: 2/1 3/2 5/3 8/5 13/8 21/13...... 求出这个数列的前N项之和,保留两位小数。
输入
N
输出
数列前N项和
样例输入
10
样例输出
16.48
提示
来源
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int f[] = new int[n + 1];
f[0] = 1;
f[1] = 2;
for (int i = 2; i <= n; i++)
f[i] = f[i - 1] + f[i - 2];
double sum = 0;
for (int i = 0; i < n; i++)
sum += (double) ((f[i + 1] * 1.0) / f[i]);
System.out.printf("%.2f", sum);
}
}