第十届蓝桥杯(省赛)之Fibonacci数列和黄金分割

第十届蓝桥杯(省赛)之Fibonacci数列和黄金分割_第1张图片

 一、version1

遇到的问题

  • F数组存储的数据超过long表示范围
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class ExaminationF {
    //public static int N = 2000000000;//java.lang.OutOfMemoryError: Java heap space
    public int N ;//java.lang.OutOfMemoryError: Java heap space
    public long[] F;

    public  long fibonacci(int n){
        if (n == 1 || n == 2){
            return 1;
        }
        if (F[n] != 0) {
            return F[n];
        }
        if (F[n] == 0)
            F[n] = fibonacci(n-1)+fibonacci(n-1);
        return F[n];
    }

    public ExaminationF(int n) {
        N = n;
        F = new long[N+5];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        ExaminationF examinationF = new ExaminationF(n);
        //System.out.println(examinationF.F.length);

        examinationF.F[1] = examinationF.F[2] = 1;
        examinationF.fibonacci(n+1);

        System.out.println(String.format("%7f",examinationF.F[n]*1.0/ examinationF.F[n+1]));
        System.out.println(new BigDecimal(examinationF.F[n]*1.0/examinationF.F[n+1]+"").setScale(8));
    }
}

二、version2

遇到的问题

  • java中的默认类型:整数类型是int、浮点类型是double。
    当 n 越大时,两数之间的除法越接近黄金比例,也就是说 n 越大,最后的结果变化越来越小,即数值的变化离小数点越远
    i = 21之后小数点后10位不再变化
    float类型有8位有效数字,第七位将产生四舍五入(5及5以下的都将舍去
public class ExaminationF_2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long[] F = new long[n+5];

        F[1] = F[2] = 1;
        for (int i = 3; i <= n+1; i++) {
            F[i] = F[i-1] + F[i-2];
            System.out.println("i="+i+"\t"+new BigDecimal(F[i-1]*1.0/F[i]+"").setScale(8,BigDecimal.ROUND_HALF_UP));
        }
    }
}
//30
//i=3  0.50000000
//i=4  0.66666667
//i=5  0.60000000
//i=6  0.62500000
//i=7  0.61538462
//i=8  0.61904762
//i=9  0.61764706
//i=10 0.61818182
//i=11 0.61797753
//i=12 0.61805556
//i=13 0.61802575
//i=14 0.61803714
//i=15 0.61803279
//i=16 0.61803445
//i=17 0.61803381
//i=18 0.61803406
//i=19 0.61803396
//i=20 0.61803400
//i=21 0.61803399
//i=22 0.61803399
//i=23 0.61803399
//i=24 0.61803399
//i=25 0.61803399
//i=26 0.61803399
//i=27 0.61803399
//i=28 0.61803399
//i=29 0.61803399
//i=30 0.61803399
//i=31 0.61803399

三、version

import java.math.BigDecimal;
import java.util.Scanner;

public class ExaminationF_3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long[] F = new long[n+5];

        F[1] = F[2] = 1;
        if (n <= 20) {
            for (int i = 3; i <= n+1; i++)
                F[i] = F[i-1] + F[i-2];
            System.out.println(new BigDecimal(F[n]*1.0/F[n+1]+"").setScale(8,BigDecimal.ROUND_HALF_UP));
        }else {
            System.out.println(0.61803399);
        }

    }
}

 

参考材料

蓝桥杯:Fibonacci数列与黄金分割_尐镇做题家的博客-CSDN博客

你可能感兴趣的:(蓝桥杯,蓝桥杯,职场和发展,算法)