斐波那契数列

来源:互联网 发布:国内文献数据库 编辑:程序博客网 时间:2024/06/04 19:01

斐波那契数列

斐波那契数列 ———–递归
public class E09_Fibonacci { static int fib(int n) { if (n <= 2) return 1; return fib(n-1) + fib(n-2); } public static void main(String[] args) {
// Get the max value from the command line:
int n = Integer.parseInt(args[0]);
if(n < 0) {
System.out.println(“Cannot use negative numbers”);
return; }
for(int i = 1; i <= n; i++)
System.out.print(fib(i) + “, “);
} }
/* Output: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, *///:~ This problem, commonly presen

0 0
原创粉丝点击