找回密码
 立即注册
搜索
查看: 246|回复: 0

java程序斐波纳契数列

[复制链接]

43

主题

27

回帖

91

积分

注册会员

积分
91
发表于 2004-10-20 17:50:58 | 显示全部楼层 |阅读模式
/**
首先介绍一下什么叫做Fibonacci(斐波纳契数列,它是一个整数数列,其中每数等于前两数之和)
* This program prints out the first 20 numbers in the Fibonacci sequence.
* Each term is formed by adding together the previous two terms in the
* sequence, starting with the terms 1 and 1.
这个程序列出了20个斐波纳契数,从1开始,每一个数都等于前两个数之和)
**/
public class Fibonacci {
    public static void main(String[  ] args) {
        int n0 = 1, n1 = 1, n2;          // Initialize variables
        System.out.print(n0 + " " +      // Print first and second terms
                         n1 + " ");      // of the series

        for(int i = 0; i < 18; i++) {    // Loop for the next 18 terms
            n2 = n1 + n0;                // Next term is sum of previous two
            System.out.print(n2 + " ");  // Print it out
            n0 = n1;                     // First previous becomes 2nd previous
            n1 = n2;                     // And current number becomes previous 先计算出第3个数,然后依次将后一个数的值赋给前一个数
        }
        System.out.println( );            // Terminate the line
    }
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|海浩社区

GMT+8, 2025-9-19 18:11 , Processed in 0.069591 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表