java基础

/*
* 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);

java基础
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");
    }

}
java基础

java基础,布布扣,bubuko.com

java基础

上一篇:【python】pprint格式化输出


下一篇:java中会存在内存泄漏吗,请简单描述。