Browse Source

上传文件至 ''

java代码
管理员 7 years ago
parent
commit
56240c64e1
1 changed files with 54 additions and 0 deletions
  1. 54 0
      Test.java

+ 54 - 0
Test.java

@@ -0,0 +1,54 @@
+package me.yoqi;
+
+import java.math.BigInteger;
+
+public class Test {
+
+	public static void main(String args[]) {
+		int init = 25;
+		double pk = 0;
+		for (int n = 3; n < init; n++) {
+			BigInteger nn = new BigInteger(String.valueOf(Math.pow(365, n)));
+			long temp = (combination(365, n).multiply(factorial(n)).divide(nn)).longValue();
+			pk = 1 - temp;
+			if (pk >= 0.5) {
+				System.out.println("最小K:" + n);
+				break;
+			}
+		}
+		System.out.print(factorial(25));
+	}
+
+	/**
+	 * 计算阶乘数,即n! = n * (n-1) * ... * 2 * 1
+	 * 
+	 * @param n
+	 * @return
+	 */
+	private static BigInteger factorial(int n) {
+		BigInteger nn=new BigInteger(Integer.toString(n));
+		return (BigInteger) ((n > 1) ? factorial(n - 1).multiply(nn) : 1);
+	}
+
+	/**
+	 * 计算排列数,即A(n, m) = n!/(n-m)!
+	 * 
+	 * @param n
+	 * @param m
+	 * @return
+	 */
+	public static BigInteger arrangement(int n, int m) {
+		return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)) : 0);
+	}
+
+	/**
+	 * 计算组合数,即C(n, m) = n!/((n-m)! * m!)
+	 * 
+	 * @param n
+	 * @param m
+	 * @return
+	 */
+	public static BigInteger combination(int n, int m) {
+		return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)).divide(factorial(m)) : 0);
+	}
+}