📄 mathutil.java
字号:
package org.xinguan.common.util;
import java.util.*;
final public class MathUtil
{
final static private int PN_MAX=20;
final static private int C_MAX=66;
final static public int COMM_BNEX=60;
final static private String[] NEX=new String[]{"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
final static private HashMap<String,Integer> NEXHASH=new HashMap<String,Integer>();
final static private HashMap<String,Long> nmathValues=new HashMap<String,Long>(30);
final static private HashMap<String,Long> pmathValues=new HashMap<String,Long>(300);
final static private HashMap<String,Long> cmathValues=new HashMap<String,Long>(3000);
static
{
for(int idx=0;idx<NEX.length;idx++)
{
NEXHASH.put(NEX[idx],idx);
}
nmathValues.put("N0",1l);
for(int bidx=1;bidx<=PN_MAX;bidx++)
{
nmathValues.put("N"+bidx,nmathValues.get("N"+(bidx-1))*bidx);
pmathValues.put("0P"+bidx,0l);
pmathValues.put(bidx+"P"+bidx,nmathValues.get("N"+bidx));
pmathValues.put("1P"+bidx,(long)bidx);
for(int cidx=2;cidx<PN_MAX&&cidx<bidx;cidx++)
{
pmathValues.put(cidx+"P"+bidx,(bidx-cidx+1)*pmathValues.get((cidx-1)+"P"+bidx));
}
}
for(int bidx=0;bidx<=C_MAX;bidx++)
{
cmathValues.put("0C"+bidx,1l);
cmathValues.put(bidx+"C"+bidx,1l);
for(int cidx=1;cidx<=bidx-1;cidx++)
{
cmathValues.put(cidx+"C"+bidx,cmathValues.get(cidx+"C"+(bidx-1))+cmathValues.get((cidx-1)+"C"+(bidx-1)));
}
}
}
static public String int2nex(final int value,int base)
{
if(base<=1||base>NEX.length)
{
throw new RuntimeException("非法的数值转换基数["+base+"]....");
}
StringBuffer buffer=new StringBuffer();
if(value<0)
{
buffer.append("-");
}
int tmpvalue=value;
tmpvalue=Math.abs(value);
Stack<Integer> ints=new Stack<Integer>();
while(tmpvalue!=0)
{
ints.push(tmpvalue%base);
tmpvalue=tmpvalue/base;
}
if(ints.size()>0)
{
while(ints.size()>0)
{
buffer.append(NEX[ints.pop()]);
}
}
else
{
buffer.append(NEX[0]);
}
return buffer.toString();
}
static public int nex2int(final String value,int base)
{
if(base<=1||base>NEX.length)
{
throw new RuntimeException("非法的数值转换基数["+base+"]....");
}
String tmpvalue=new String(value);
int sign=tmpvalue.startsWith("-")?-1:1;
if(sign<0)
{
tmpvalue=tmpvalue.substring(1);
}
int tmp=0;
for(int i=0;i<tmpvalue.length();i++)
{
String fact=tmpvalue.substring(i,i+1);
if(!NEXHASH.containsKey(fact))
{
throw new RuntimeException("数值转换中数值超过基数");
}
tmp=(tmp+NEXHASH.get(fact))*base;
}
return sign*tmp/base;
}
static public String long2nex(final long value,int base)
{
if(base<=1||base>NEX.length)
{
throw new RuntimeException("非法的数值转换基数["+base+"]....");
}
long tmpvalue=value;
StringBuffer buffer=new StringBuffer();
if(tmpvalue<0)
{
buffer.append("-");
}
tmpvalue=Math.abs(tmpvalue);
Stack<Integer> ints=new Stack<Integer>();
while(tmpvalue!=0)
{
ints.push((int)(tmpvalue%base));
tmpvalue=tmpvalue/base;
}
if(ints.size()>0)
{
while(ints.size()>0)
{
buffer.append(NEX[ints.pop()]);
}
}
else
{
buffer.append(NEX[0]);
}
return buffer.toString();
}
static public long nex2long(String value,int base)
{
if(base<=1||base>NEX.length)
{
throw new RuntimeException("非法的数值转换基数["+base+"]....");
}
String tmpvalue=new String(value);
int sign=tmpvalue.startsWith("-")?-1:1;
if(sign<0)
{
tmpvalue=tmpvalue.substring(1);
}
long tmp=0;
for(int i=0;i<tmpvalue.length();i++)
{
String fact=tmpvalue.substring(i,i+1);
if(!NEXHASH.containsKey(fact))
{
throw new RuntimeException("数值转换中数值超过基数");
}
tmp=(tmp+NEXHASH.get(fact))*base;
}
return sign*tmp/base;
}
static public synchronized long _N(int base)
{
if(base>PN_MAX)
{
throw new RuntimeException("N基数超过最大值");
}
if(nmathValues.containsKey("N"+base))
{
return nmathValues.get("N"+base);
}
long value=1l;
for(int idx=2;idx<=base;idx++)
{
value*=idx;
}
nmathValues.put("N"+base,value);
return nmathValues.get("N"+base);
}
static public synchronized long _P(int exp,int base)
{
if(base>PN_MAX||exp>PN_MAX||exp>base)
{
throw new RuntimeException("P基数或指数超过最大值或者参数非法");
}
if(pmathValues.containsKey(exp+"P"+base))
{
return pmathValues.get(exp+"P"+base);
}
long value=_N(base);
value=value/_N(base-exp);
pmathValues.put(exp+"P"+base,value);
return pmathValues.get(exp+"P"+base);
}
static public synchronized long _C(int exp,int base)
{
if(base>C_MAX||exp>C_MAX||exp>base)
{
throw new RuntimeException("C基数或指数超过最大值或者参数非法");
}
if(cmathValues.containsKey(exp+"C"+base))
{
return cmathValues.get(exp+"C"+base);
}
long value=_C(exp-1,base-1)+_C(exp,base-1);
cmathValues.put(exp+"C"+base,value);
return cmathValues.get(exp+"C"+base);
}
@SuppressWarnings("unchecked")
public static List sort(List list)
{
Object[] objects=list.toArray();
Arrays.sort(objects);
list.clear();
for(Object object:objects)
{
list.add(object);
}
return list;
}
public static Object[] sort(Object[] array)
{
Arrays.sort(array);
return array;
}
public static int usbyte(byte b)
{
return b<0?(int)(256+b):b;
}
public static int byte2Int(byte[] bytes)
{
if(bytes.length>4)
{
throw new RuntimeException("");
}
int value=0;
for(int idx=0;idx<bytes.length;idx++)
{
value=value+(((int)(bytes[idx]&0xff))<<(8*(bytes.length-idx-1)));
}
return value;
}
public static int byte2Int(byte[] bytes,int start,int len)
{
if(len>4)
{
throw new RuntimeException("");
}
int value=0;
for(int idx=0;idx<len&&idx<(bytes.length-start);idx++)
{
value=value+(((int)(bytes[idx+start]&0xff))<<(8*(len-idx-1)));
}
return value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -