一个女人生小孩的概率 问题
2007-07-11 08:45
260 查看
/**//*一个女人生小孩的概率 问题
总共生四个小孩
a 都是 男的 或都是女的
b 两个男的 和两个女的
c 三个男的和一个女的 或三个女的和一个男的
求a b c 的概率
只用到 if 和 for语句
和Math.random这个函数!~ */
import java.text.DecimalFormat;
public class Probability ...{
public int totalCount = 100000;// 总循环次数
DecimalFormat myFormat = new DecimalFormat("#0.0000");
// 都是 男的 或都是女的
public void viewA() ...{
int okCount = 0;
for (int i = 0; i < totalCount; i++) ...{
int temp = 0;
for (int j = 0; j < 4; j++)
temp += (int) (Math.random() * 10 + 1);
if (temp == 40 || temp == 4)
okCount++;
}
double d = (1.0 * okCount / totalCount + 0.0000005) * 1000;
//System.out.println(d + " " + okCount);
System.out.println(myFormat.format(d) + "/" + (totalCount * 1000));
}
// 两个男的 和两个女的
public void viewB() ...{
int okCount = 0;
for (int i = 0; i < totalCount; i++) ...{
int man = 0, woman = 0;
int[] temp = ...{ (int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1) };
for (int j = 0; j < 4; j++) ...{
if (temp[j] == 10)
man++;
else if (temp[j] == 1)
woman++;
else
continue;
}
if (man == 2 && woman == 2)
okCount++;
}
double d = (1.0 * okCount / totalCount + 0.0000005) * 1000;
System.out.println(myFormat.format(d) + "/" + (totalCount * 1000));
}
// 三个男的和一个女的 或三个女的和一个男的
public void viewC() ...{
int okCount = 0;
for (int i = 0; i < totalCount; i++) ...{
int man = 0, woman = 0;
int[] temp = ...{ (int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1),
(int) (Math.random() * 10 + 1) };
for (int j = 0; j < 4; j++) ...{
if (temp[j] == 10)
man++;
else if (temp[j] == 1)
woman++;
else
continue;
}
if ((man == 3 && woman == 1) || (man == 1 && woman == 3))
okCount++;
}
double d = (1.0 * okCount / totalCount + 0.0000005) * 1000;
System.out.println(myFormat.format(d) + "/" + (totalCount * 1000));
}
public static void main(String[] args) ...{
Probability p = new Probability();
p.viewA();
p.viewB();
p.viewC();
}
}
相关文章推荐
- 猎人和狼,一个男人带两个小孩, 一个女人带两个小孩过河问题图解
- 欺诈游戏中的一个概率问题
- 一个n个整数序列取m个数,使其概率相同问题
- C++中的不定层迭代,以若干面骰子概率问题为例
- 概率与随机函数问题
- LightOJ1265---Island of Survival (概率问题)
- 接下班概率问题——阿里笔试题
- 找东西背后的概率问题
- 数学问题——调整概率
- 传球(概率问题)
- 概率相关问题
- 概率随机问题
- 一个简单的条件概率问题
- Robberies(HDU2955):01背包+概率转换问题(思维转换)
- 数学概念——F 概率(经典问题)birthday paradox
- 蒙特卡罗模拟(使用随机数和概率来解决问题)
- 一个概率小问题
- HDU 5985 概率问题
- 概率问题 _蓝桥杯
- 动态规划 ③ HDU - 1203 I NEED A OFFER! 背包问题+独立事件概率