求斐波那契数列第n项的值和前n项的和
作者:互联网
1、
有一串数字:0 1 1 2 3 5 8 13 21 34 55
求该数列的第n项的值和前n项的和:
package test02;
public class Fore {
public static void main(String[] args) {
int n = 8;
int []f = new int[n];
int sum=1;
f[0]=0;
f[1]=1;
f[2]=1;
for (int i = 2;i<n;i++){
f[i]=f[i-1]+f[i-2];
sum=sum+f[i];
if(i==n-1) {
System.out.println("第n项的值为:" + f[i]);
}
}
System.out.println("前n项的和为:" + sum);
}
标签:求该,求斐波,数列,int,static,那契,public,21 来源: https://blog.csdn.net/weixin_42077253/article/details/101057369