基本的整数和浮点数精度不过,可以使用java.math包中的BigInteger和BigDecimal
BigInteger add(BigInteger otherBigInteger subtract(BigInteger other)BigInteger multiply(BigInteger other)BigInteger divide(BigInteger other)-
BigInteger mod(BigInteger other)
返回这个大整数和另一个大整数的和差积商余 -
int compareTo(BigInteger other)
这个大整数和另一个大整数相等返回0;大于返回正数;小于返回负数
*static BigInteger valueOf(long x)
返回值等于x - 四舍五入方法:
java.Math.roundingMode.HALF_UP
import java.math.*;
import java.util.*;
public class BigIntegerTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("How many numbers do you need to draw? ");
int k = in.nextInt();
System.out.print("What is the highest number you can draw? ");
int n = in.nextInt();
/*
*计算二项式系数n*(n-1)*(n-2)*…(n-k+1)/(1*2*3*…*k)
*/
BigInteger lotteryOdds = BigInteger.valueOf(1);
for (int i = 1; i <= k; i++)
lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(BigInteger.valueOf(i));
//大数值的加减乘除都用方法
System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
}
}











网友评论