这个排列还需要递归?一个简单的循环就够啦另外,如果需要精确,则最好使用BigInteger,如果不需要则把BigInteger改成double public static BigInteger p(int down, int up) { if (down < 0 || up < 0 || down < up) throw new IllegalArgumentException("上标下标为正数,且上标大于下标"); BigInteger result = BigInteger.ONE; for(int i = down - up + 1; i <= down; i++) result = result.multiply(BigInteger.valueOf(i)); return result; }
public static BigInteger c(int down, int up) { if (down < 0 || up < 0 || down < up) throw new IllegalArgumentException("上标下标为正数,且上标大于下标"); BigInteger result = BigInteger.ONE; for(int i = down - up + 1; i <= down; i++) result = result.multiply(BigInteger.valueOf(i)); for(int i = 1; i <= up; i++) result = result.divide(BigInteger.valueOf(i)); return result; }
-------
m!或者说 60 * 59 * ...* 32 * 31
public static long p(long arg0,long arg1) throws Exception{
if(arg0<0 || arg1<0 || arg0 <arg1)throw new Exception("上标下标为正数,且上标大于下标");
if(arg0 == arg1){
return 0;
}else if(arg0 == arg1 +1){
return arg0*arg1;
}else{
return arg0*p(arg0-1,arg1)/(arg0-arg1);
}
}
public static void main(String[] args){
try {
System.out.println(Permutation.p(60,2));
} catch (Exception e) {
System.out.println(e);
}
}
}
public static BigInteger p(int down, int up) {
if (down < 0 || up < 0 || down < up)
throw new IllegalArgumentException("上标下标为正数,且上标大于下标");
BigInteger result = BigInteger.ONE;
for(int i = down - up + 1; i <= down; i++)
result = result.multiply(BigInteger.valueOf(i));
return result;
}
public static BigInteger c(int down, int up) {
if (down < 0 || up < 0 || down < up)
throw new IllegalArgumentException("上标下标为正数,且上标大于下标");
BigInteger result = BigInteger.ONE;
for(int i = down - up + 1; i <= down; i++)
result = result.multiply(BigInteger.valueOf(i));
for(int i = 1; i <= up; i++)
result = result.divide(BigInteger.valueOf(i));
return result;
}