Java程序以打印斐波那契数列
Fibonacci系列通过将两个先前的数字相加来生成后续的数字。斐波那契数列从两个数字开始-F0和F1。F0的初始值F1可以分别取0、1或1、1。
Fn = Fn-1 + Fn-2
算法
1. Take integer variable A, B, C 2. Set A = 1, B = 1 3. DISPLAY A, B 4. C = A + B 5. DISPLAY C 6. Set A = B, B = C 7. REPEAT from 4 - 6, for n times
示例
public class FibonacciSeries2{ public static void main(String args[]) { int a, b, c, i, n; n = 10; a = b = 1; System.out.print(a+" "+b); for(i = 1; i <= n-2; i++) { c = a + b; System.out.print(" "); System.out.print(c); a = b; b = c; } } }
输出结果
1 1 2 3 5 8 13 21 34 55