NO.1
斐波那契数列的通项公式:f(n)=1(n=1||n=2) ①; f(n)=f(n-1)+f(n-2)(n>2) ②
public class Fibonacci {
public static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
public static void fibonacci(int n) {
for (int x = 1; x < n; x++) {
if (x < (n - 1)) {
System.out.print(fib(x) + ",");
}else{
System.out.println(fib(x));
}
}
}
public static void main(String[] args) {
fibonacci(100);
}
}
|