Blue Bridge dice game JAVA

Let's play a game.
While ordinary throw three dice (figure 6 faces are 1 to 6).
If one of the numbers on a dice equal to the other two and, you win.

The following program you can calculate the exact probability of winning (expressed as irreducible fraction)
thinking: There are three of them and two equal third largest pit is this for loop 0, 1 sieve from the beginning, so to add a.

	public static int gcd(int a, int b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	public static void main(String[] args) {
		int n = 0;
		for (int i = 0; i < 6; i++)
			for (int j = 0; j < 6; j++)
				for (int k = 0; k < 6; k++) {
					if ((i + 1) == (j + 1) + (k + 1)|| (j + 1) == (i + 1) + (k + 1)|| (k + 1) == (i + 1) + (j + 1)) {
						n++; // 填空位置
					}

				}

		int m = gcd(n, 6 * 6 * 6);
		System.out.println(n / m + "/" + 6 * 6 * 6 / m);
	}
i == j + k + 1 || j == i + k + 1 || k == i + j + 1   //填空位置	

Small Theater: Some things are not going just to see the hope stick, but insisted the only hope.

Published 108 original articles · won praise 113 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43771695/article/details/104612012