package cn.itcast.girl.TheBlueCup_02;
import java.util.Scanner;
public class CastAccount {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double f = 0;
f = (Math.pow(((1+Math.sqrt(5))/2), n) - Math.pow(((1-Math.sqrt(5))/2), n))/Math.sqrt(5);
System.out.printf("%.2f",f);
sc.close();
}
}
这个Java程序计算并输出斐波那契数列的第n项,使用了数学公式来直接求解,避免了递归或迭代的复杂性。用户输入一个整数n,程序将计算结果保留两位小数并打印。
605

被折叠的 条评论
为什么被折叠?



