有没有办法把平局也放进去??请问:有没有办法把平局也放进去
CASE 1 A - B >= 2
Case 2 B - A >= 2
Case 3 A = B ---> 放回原先的循环
System.out.println((double) aWin * 100 / n + "%");
另外可否print bWin% ? draw?
每场比赛要有局数限制才会产生平局的情况吧?
如果加入每场比赛局数限制,修改后的代码如下:
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为比赛场数
int m = (new Scanner(System.in)).nextInt(); // m为每场比赛回合数
for (int i = 0; i < n; i++)
{
int aScore = 0, bScore = 0, j = 0;
while (Math.abs(aScore - bScore) < 2 && j < m)
{
int game = Math.abs(random.nextInt() % 3);
if (game == 0 || game == 1)
aScore++;
else
bScore++;
if (aScore - bScore >= 2)
aWin++;
else if (bScore - aScore >= 2)
bWin++;
j++;
}
}
System.out.println((double) aWin / n * 100 + "%\n" +
(double) bWin / n * 100 + "%\n" + (100 - (double) (aWin + bWin) / n * 100) + "%\n");
}
}
如果限制10局,甲胜的概率貌似接近78.6几,几个测试结果:
100
10
75.0%
25.0%
0.0%
1000
10
79.10000000000001%
19.6%
1.2999999999999972%
10000
10
78.59%
19.45%
1.9599999999999937%
100000
10
78.513%
19.744999999999997%
1.7420000000000044%
10000000
10
78.61402%
19.653409999999997%
1.7325699999999955%
10000000
10
78.63001%
19.63833%
1.731660000000005%
10000000
10
78.60665%
19.65362%
1.7397299999999944%
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为比赛场数
int m = (new Scanner(System.in)).nextInt(); // m为每场比赛回合数
for (int i = 0; i < n; i++)
{
int aScore = 0, bScore = 0, j = 0;
while (Math.abs(aScore - bScore) < 2 && j < m)
{
int game = Math.abs(random.nextInt() % 3);
if (game == 0 || game == 1)
aScore++;
else
bScore++;
if (aScore - bScore >= 2)
aWin++;
else if (bScore - aScore >= 2)
bWin++;
j++;
}
}
System.out.println((double) aWin / n * 100 + "%\n" +
(double) bWin / n * 100 + "%\n" + (100 - (double) (aWin + bWin) / n * 100) + "%\n");
}
}
如果限制10局,甲胜的概率貌似接近78.6几,几个测试结果:
100
10
75.0%
25.0%
0.0%
1000
10
79.10000000000001%
19.6%
1.2999999999999972%
10000
10
78.59%
19.45%
1.9599999999999937%
100000
10
78.513%
19.744999999999997%
1.7420000000000044%
10000000
10
78.61402%
19.653409999999997%
1.7325699999999955%
10000000
10
78.63001%
19.63833%
1.731660000000005%
10000000
10
78.60665%
19.65362%
1.7397299999999944%