AcWing 741. 斐波那契数列 - Java
原题链接
简单
作者:
KYCygni
,
2021-03-16 05:26:06
,
所有人可见
,
阅读 272
Java 代码
import java.util.Scanner;
public class Main
{
public static long getFib(int n)
{
long[] f = new long[n+1];
f[0] = 0;
for (int i = 1; i < n+1; i ++)
{
if (i == 1)
f[i] = 1;
else
f[i] = f[i-2] + f[i-1];
}
return f[n];
}
public static void main (String[] args)
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for (int i = 0; i < n; i ++)
{
int x = cin.nextInt();
System.out.println ("Fib(" + x + ") = " + getFib(x));
}
}
}