Java面向对象的方式对斐波那契数列求和

文章目录

  • 前言
  • 一、使用步骤
    • 1.引入库
  • 总结


前言

面向对象的方式对斐波那契数列进行求和,本人的思路是先把一定大小的斐波那契数列构造成集合,在对集合进行求和操作。


一、使用步骤

1.引入库

代码如下:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int sum=getSumFromFeibo(5);
        System.out.println(sum);
    }
    
    // 1 1 2 3 5 8 ...

    /**
     * 对斐波那契数列数列求和
     * @param feiboArrSize
     * @return
     */
    public static int getSumFromFeibo(int feiboArrSize){
        if (feiboArrSize<=0){
            return 0;
        }

        // 0 1 1 2 3 5 8 ...
        /** pre     next    current
         *  0       1       1
         *  1       1       2
         *  1       2       3
         *  2       3       5
         *  3       5       8
         *  5       8       13
         *  ...
         */
        List<FeiboEle> feiboEleList=new ArrayList<FeiboEle>();
        FeiboEle feiboEle=new FeiboEle();
        feiboEle.setFeiboElePrevious(0);
        feiboEle.setFeiboElePreNext(1);
        feiboEle.setFeiboEleCurrent(1);
        feiboEleList.add(feiboEle);

        for (int i = 0; i < feiboArrSize; i++) {

            FeiboEle feiboEleTemp=feiboEleList.get(i);
            int feiboElePre=feiboEleTemp.getFeiboElePrevious();
            int feiboEleNext=feiboEleTemp.getFeiboElePreNext();
            int feiboEleCurrent=feiboEleTemp.getFeiboEleCurrent();

            int feiboEleCurrentTemp=feiboEleNext+feiboEleCurrent;
            FeiboEle feiboEleTemp1=new FeiboEle();
            feiboEleTemp1.setFeiboElePrevious(feiboEleNext);
            feiboEleTemp1.setFeiboElePreNext(feiboEleCurrent);
            feiboEleTemp1.setFeiboEleCurrent(feiboEleCurrentTemp);

            feiboEleList.add(feiboEleTemp1);
        }

        List<Integer> intEleList=new ArrayList<Integer>();
        for (FeiboEle ele : feiboEleList) {
            int feiboEleTemp=ele.getFeiboElePrevious();
            intEleList.add(feiboEleTemp);
        }

        // 1 1 2 3 5 8 ...
        System.out.println(feiboEleList);

        System.out.println("----------");

        System.out.println(intEleList);

        int sum=0;
        for (Integer integer : intEleList) {
            sum+=integer;
        }

       return sum;
    }
}

class FeiboEle{
    private Integer FeiboElePrevious;
    private Integer FeiboElePreNext;
    private Integer FeiboEleCurrent;

    public Integer getFeiboElePrevious() {
        return FeiboElePrevious;
    }

    public void setFeiboElePrevious(Integer feiboElePrevious) {
        FeiboElePrevious = feiboElePrevious;
    }

    public Integer getFeiboElePreNext() {
        return FeiboElePreNext;
    }

    public void setFeiboElePreNext(Integer feiboElePreNext) {
        FeiboElePreNext = feiboElePreNext;
    }

    public Integer getFeiboEleCurrent() {
        return FeiboEleCurrent;
    }

    public void setFeiboEleCurrent(Integer feiboEleCurrent) {
        FeiboEleCurrent = feiboEleCurrent;
    }

    @Override
    public String toString() {
        return "FeiboEle{" +
                "FeiboElePrevious=" + FeiboElePrevious +
                ", FeiboElePreNext=" + FeiboElePreNext +
                ", FeiboEleCurrent=" + FeiboEleCurrent +
                '}';
    }
}

总结

面向对象的方式对斐波那契数列求和可读性更好,传递的参数是斐波那契数列的大小,打印出来的是斐波那契数列数列的元素。

你可能感兴趣的:(Java,算法,源码,分析,java,开发语言,算法)