剑三95和尚pve秘籍:计算机编程,java,抽奖问题
来源:百度文库 编辑:中科新闻网 时间:2024/05/10 18:58:31
import java.math.*;
import java.util.*;
public class lotteryOdds
{
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();
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!");
}
}
12月14日
JAVA - 福彩概率计算2
上回用的是普通的整型,福彩的概率计算出来的答案超出其范围了。所以,必须使用BigInterger。
计算出来的概率是 1/6724520
=========
import javax.swing.*;
import java.math.*;
public class BigIntergerTest
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("How many numbers do you need to draw?");
int k = Integer.parseInt(input); //输入需要选择号码数量,福彩是7个
input = JOptionPane.showInputDialog("What is the highest number you can draw?");
int n = Integer.parseInt(input); //输入最大数值,福彩是35
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!"); //输出中一等奖的概率
System.exit(0);
}
}