现在的位置: 首页 > 综合 > 正文

14-斐波那契(算法)

2013年08月24日 ⁄ 综合 ⁄ 共 280字 ⁄ 字号 评论关闭
斐波那契数列指的是这样一个数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
这个数列从第三项开始,每一项都等于前两项之和。
用Java写的斐波那契的前20项。
/*
 * 斐波那契
 */
public class FeiBoNum {
	public static void main(String[] args) {
	   int a =1;
	   int b=1;
	   int c=0;
	   for(int i=0;i<20;i++){
		   if(i<2){
			   System.out.print(1+"\t");
		   }else{
			   c=a+b;
			   System.out.print(c+"\t");
			   a=b;
			   b=c;
		   }
	   }
	}

}

抱歉!评论已关闭.