📄 factcache.java
字号:
package MyNa.utils;
import java.math.BigInteger;
import java.util.*;
public class FactCache extends Cache {
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));
}
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;
}
private static Cache instance=null; // the one and only class instance
private static int clients=0; // how many are asking us?
protected void init(){super.init();} // e.g., register, init logger
public static synchronized Cache getInstance(){
if(null==instance)instance=new FactCache();
clients++;
return instance;
}
public static synchronized int freeInstance(){
if(null==instance)return 0;
clients--;
if(clients==0)close(); // all gone, and the cache with it.
return clients;
}
public static synchronized boolean close(){
clients=0;instance=null;
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -