应该是80%吧,一个小程序模拟进行10000000场比赛输出结果接近80%
所在版块:求学狮城 发贴时间:2008-12-03 11:08  评分:

用户信息
复制本帖HTML代码
高亮: 今天贴 X 昨天贴 X 前天贴 X 
java代码如下:

import java.util.*;

public class Tennis
{
public static void main(String[] args)
{
Random random = new Random();
int aWin = 0, bWin = 0;
int n = (new Scanner(System.in)).nextInt(); // n为自定义比赛次数
for (int i = 0; i < n; i++)
{
int aScore = 0, bScore = 0; // 每场比赛开始时甲乙得分各为0
while (Math.abs(aScore - bScore) < 2) // 直到有人领先2分时,该场比赛结束
{
int game = Math.abs(random.nextInt() % 3); // 随机生成0,1,2间的一个整数
if (game == 0 || game == 1) // 每盘比赛甲胜出的几率是2/3
aScore++; // 甲得一分
else
bScore++; // 否则乙得一分
if (aScore - bScore >= 2) // 若甲领先2分,甲胜一场比赛
aWin++;
else if (bScore - aScore >= 2) // 若乙领先2分,乙胜一场
bWin++;
}
}
System.out.println((double) aWin * 100 / n + "%");
}
}

以下是几个测试结果:
100
84.0%
1000
81.5%
100000
79.84%
10000000
79.98379%
.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!

 相关帖子 我要回复↙ ↗回到正文
有趣的网球输赢概率题 spinach   (168 bytes , 1078reads )
Solve the Discrete Time Markov Chain simon   (0 bytes , 466reads )
题目修改 spinach   (308 bytes , 286reads )
0.855967? hash   (119 bytes , 314reads )
应该是80%吧,一个小程序模拟进行10000000场比赛输出结果接近80% MetalGearSnake   (976 bytes , 403reads )
有没有办法把平局也放进去?? spinach   (186 bytes , 271reads )
每场比赛要有局数限制才会产生平局的情况吧? MetalGearSnake   (1321 bytes , 288reads )
if you know sth about stochastic, this is a stopping-time problem 1300cc   (102 bytes , 284reads )
试着写一般表达式,这样对吗? spinach   (79 bytes , 244reads )
what is the general formula for the stopping time? spinach   (214 bytes , 238reads )
yes. it's a typo in my first equation. should be -2 , not -1/2 1300cc   (15 bytes , 291reads )
it's in this way 1300cc   (67 bytes , 372reads )
for stopping-time problems, there are no "draw"? spinach   (0 bytes , 253reads )
no draw 1300cc   (192 bytes , 256reads )
我的答案跟大家一样4:1;可是好像不对 spinach   (521 bytes , 258reads )
如果不限制盘数的话,平局的概率为零 香陵居士   (0 bytes , 322reads )
考虑到状态稳定的时候 icky   (102 bytes , 286reads )
这个好 胡萝卜   (0 bytes , 239reads )
若不限制,甲80%,乙20%;若限制,自己算 冷色蓝天   (439 bytes , 290reads )
一场比赛多少盘?还是不限制盘数? 香陵居士   (0 bytes , 361reads )
不限盘数 spinach   (0 bytes , 264reads )
每胜一盘的[得]一分,另甲胜指甲胜整场比赛 spinach   (0 bytes , 207reads )