⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fact.java

📁 java xml开发指南(初学者推荐)Java Xml 编程指南书籍源码
💻 JAVA
字号:
package MyNa.utils;

import java.math.BigInteger;
import java.util.*;
public class Fact {

public BigInteger biV(int n){  // big-integer value conversion
  return BigInteger.valueOf(n);
}

public BigInteger fact1(int n){ // traditional recursive answer.
  if(n==0)return biV(1);
  return biV(n).multiply(fact1(n-1));
}

private static Fact instance=null; // the one and only class instance
private static int clients=0;           // how many are asking us?

private Hashtable cache=null;    // the cache for answers

private Fact(){init();}     // private constructor, only called once

private void init(){cache=new Hashtable();} // set up cache

static synchronized public Fact getInstance(){
  if(null==instance)instance=new Fact();
  clients++;
  return instance;
}
static synchronized public int freeInstance(){
  if(null==instance)return 0;
  clients--;
  if(clients==0)instance=null; // all gone, and the cache with it.
  return clients;
}

public BigInteger fact(int n){
  BigInteger biN=biV(n);
  BigInteger R=(BigInteger)cache.get(biN);
  if(null!=R)return R;
  if(n==0)return saveFact(biN,biV(1));
  return saveFact(biN,biN.multiply(fact(n-1)));
}

private synchronized BigInteger saveFact(BigInteger n,BigInteger fN){
  cache.put(n,fN);
  return fN;
}

public synchronized boolean freeSpace(int numToFree){
  Enumeration enum=cache.keys();
  while(enum.hasMoreElements() && numToFree>0){
    Object ob=enum.nextElement();
    cache.remove(ob);
    numToFree--;
    }
  return numToFree==0;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -