Chinaunix首页 | 论坛 | 博客
  • 博客访问: 695323
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: Java

2009-07-01 17:07:05

拿波里次数列:

import java.util.Vector;

public class Fibonacci{
    Vector<Integer> f = new Vector<Integer>();
    public Fibonacci(){
        f.add(1);
        f.add(1);
    }

    public void printFibonacci(int num){
        if (num > 1){
            for (int i = 0; i <= num - 2; i++){
                int tmp = Integer.parseInt((f.elementAt(i).toString())); //将对象转换为字符串,然后强制转换字符串为Integer对象,最后默认将Integer类型转换为整数型。

                int tmp2 = Integer.parseInt((f.elementAt(i + 1).toString()));
                f.add(tmp + tmp2);
            }
        }
        
        for (int j = 0; j < num; j++){
            System.out.println(f.elementAt(j));
        }
    }
    
    public static void main(String [] args){
        Fibonacci fn = new Fibonacci();
        fn.printFibonacci(5);
        
    }
}

阅读(672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~