没什么人关注啊……自顶。。。
写了一段求彩票概率的小代码……
- import java.math.*;
- import java.util.*;
- /**
- 本程序用以计算m选n的概率。其公式为:(m*(m-1)*(m-2)...*(m-(n-1))/(1*2*3..*n);
- */
- public class GaiLv
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- BigInteger result = new BigInteger("1");
- int m,n;
- System.out.println("请输入m的值:");
- m = in.nextInt();
- System.out.println("请输入n的值:");
- n = in.nextInt();
- int k;
- for(k=1;k<=n;k++)
- {
- result = result.multiply(BigInteger.valueOf(m-k+1));
- result = result.divide(BigInteger.valueOf(k));
- }
- System.out.println(m+"选"+n+"的概率为:"+result+"分之1");
- }
- }
复制代码 |