Skip to content

Commit aa4f62a

Browse files
authored
Create Three Dices.c
1 parent a63943d commit aa4f62a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Codechef Problems/Three Dices.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* PROBLEM STATEMENT
2+
3+
There are three people, and each of them has an unbiased 6-sided die. The result of rolling a die will be a number between 1 and 6 (inclusive) with equal probability.
4+
5+
The three people throw their dice simultaneously. In this game, the third person wins only if his number is strictly greater than the sum of the other two numbers. Given that the first person rolls the value X and the second person rolls the value Y, what is the probability the third person will win?
6+
7+
Input Format
8+
The first line contains an integer T, the number of test cases. Then the test cases follow.
9+
Each test case contains two integers X and Y.
10+
Output Format
11+
For each test case, output the probability that the third person wins.
12+
13+
Your answer will be considered correct if its absolute error doesn't exceed 10−6.
14+
15+
Constraints
16+
1≤T≤36
17+
1≤X,Y≤6
18+
Sample Input 1
19+
3
20+
1 3
21+
2 4
22+
2 3
23+
Sample Output 1
24+
0.333333
25+
0
26+
0.166666
27+
Explanation
28+
In the first test case, out of the six outcomes of a die, the third person wins if the result is either 5 or 6. So the probability of winning is 26≈0.333333.
29+
30+
In the second test case, the third person only wins if the result is greater than 6, which is impossible. So the probability of winning is 0. */
31+
32+
33+
#include<stdio.h>
34+
35+
int main(void) {
36+
37+
int t,x,y;
38+
//t takes the number of test cases as input
39+
scanf("%d",&t);
40+
int tc=t;
41+
float a[t]; int i=0;
42+
while(t--)
43+
{
44+
scanf("%d %d",&x,&y);
45+
// x and y are the respective outcomes on the dice of first and second player
46+
float sum=x+y;
47+
float probability= (6.0-sum)/6.0;
48+
//probability= (total-outcome)/total
49+
a[i]=probability;
50+
i++;
51+
52+
}
53+
for(int j=0;j<tc;j++)
54+
{
55+
if(a[j]>0)
56+
printf("%.6f \n",((signed long)(a[j] * 1000000) * 0.000001f));
57+
//the above statement prints the output upto 6 decimal places without rounding off the last place.(this is required for successful submission of code)
58+
else
59+
printf("0 \n");
60+
}
61+
62+
return 0;
63+
}

0 commit comments

Comments
 (0)