/*
* java里不能使用人们熟悉的算术运算符处理大数值 而需要使用大数值类中的add和multiply方法
* BigInteger
c = a.add(b)// c = a+b;
* BigInteger d =
c.multiply(b.add(BigInteger.valueOf(2)))// d = c *(b+2)
* */
如计算n*(n-1)*...*(n-k+1) / (1*2*...k);
package welcome; import java.math.BigInteger; import java.util.Scanner; public class welcome { public static void main(String[] args) { Scanner in = new Scanner(System.in); int k = in.nextInt(); int n = in.nextInt(); BigInteger lotteryOdds = BigInteger.valueOf(1); for(int i=1;i<=k;i++) { lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n-1-i)).divide(BigInteger.valueOf(i)); } System.out.println("Your odds are 1 in "+ lotteryOdds +".Good luck"); } }