从4个元素的签名开始,每个后续元素是前面4个的总和,一个Pentabonacci带有5个元素的签名,每个后续元素是前面5个元素的总和,依此类推。

If you have completed the Tribonacci sequence kata, you would know by now that mister Fibonacci has at least a bigger brother. If not, give it a quick look to get how things work.

Well, time to expand the family a little more: think of a Quadribonacci starting with a signature of 4 elements and each following element is the sum of the 4 previous, a Pentabonacci (well Cinquebonacci would probably sound a bit more italian, but it would also sound really awful) with a signature of 5 elements and each following element is the sum of the 5 previous, and so on.

Well, guess what? You have to build a Xbonacci function that takes a signature of X elements - and remember each next element is the sum of the last X elements - and returns the first n elements of the so seeded sequence.
 

代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Xbonacci {
    public static double[] xbonacci(double[] signature, int n) {
        List<Double> list=new ArrayList<>();
        List<Double> sum=new ArrayList<>();
        int length=signature.length;
        Double s=0.0;
        for(double db:signature){
            list.add(db);
            s=s+db;
        }
        list.add(s);
        while(list.size()<n){
            sum=list.subList(list.size()-length,list.size());
            getListData(length,list,sum);
        }
        double[] arr=new double[list.size()];
        for(int k=0;k<list.size();k++){
            arr[k]=list.get(k);
        }
        return arr;
    }

    public static List<Double> getListData(int length,List<Double> list,List<Double> sum){
        Double ss=0.0;
        for(int i=0;i<sum.size();i++){
            ss=ss+sum.get(i);
        }
        list.add(ss);
        return list;
    }

    public static void main(String[] args) {
        double[] d={1,0,0,0,0,0,1};
        System.out.println(Arrays.toString(xbonacci(d,10)));

    }
}

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值