Please sign in first.
公式
D - P(X or Y) 解説 by en_translator
Let us call the two dice \(A\) and \(B\). Suppose that \(A\) shows an integer \(a\) and \(B\) shows \(b\). Then there are \(36\) combinations of \((a, b)\): \((1,1),(1,2),\ldots,(6,5),(6,6)\). Exactly one of these \(36\) events will happen with equal probability \(\dfrac1{36}\), so the problem can be solved by counting how many of the \(36\) pairs satisfies the condition.
The sample code is shown below.
Since your answer is considered correct if the error is at most \(10 ^ {-9}\), in some languages you may need to specify how many decimal places to print.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int X, Y;
cin >> X >> Y;
int count = 0; // Counter for conforming pairs
for (int a = 1; a <= 6; ++a) // Number shown by the first die
for (int b = 1; b <= 6; ++b) // Number shown by the second die
if (a + b >= X || a + Y <= b || b + Y <= a) // If it satisfies the condition
++count; // increment the counter
// The answer is obtained by dividing by 36
cout << setprecision(10) << count / 36. << endl;
return 0;
}
X, Y = map(int, input().split())
count = 0
for a in range(1, 7):
for b in range(1, 7):
if a + b >= X or a + Y <= b or b + Y <= a:
count += 1
print(count / 36)
投稿日時:
最終更新: