Java数组预处理实现费切那波数列
2016-05-24 20:00
507 查看
下面我们展示一段Java实现费切那波数列的代码。有两个特点,它将已经运算过的数存在数组中,从而避免了重复运算;还有采用的big integer来存储,可存储的数据更大。
import java.math.BigInteger;
import java.util.Scanner;
public class Experiment1
{
static BigInteger f[] = new BigInteger[200];
}
import java.math.BigInteger;
import java.util.Scanner;
public class Experiment1
{
static BigInteger f[] = new BigInteger[200];
public static void main(String[]args) { f[0]=new BigInteger("1"); f[1]=new BigInteger("2"); for(int i=2;i<f.length;i++) { f[i]=new BigInteger("0"); } Scanner scan=new Scanner(System.in); while(scan.hasNext()) { int n=scan.nextInt(); printFibonacciArray( n-1); System.out.println(f[n-1]); } } public static BigInteger printFibonacciArray(int n) {//the first number, the second number,the totel fibonacci numbers if(n==0) { return f[0]; } else if(n==1) return f[1]; else { if(f .compareTo(new BigInteger("0"))!=0) return f ; else { f =printFibonacciArray(n-1).add(printFibonacciArray(n-2)); return f ; } } }
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树