
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Ways Two Players Win or Draw in Dice Throwing Game
Suppose we have two numbers a and b. Amal and Bimal are playing a game. First each of them writes an integer from 1 to 6, then a dice is thrown. The player whose written number got closer to the number written on the paper, he wins that round, if both of them has the same difference, then that is a draw. If Amal writes the number a, and Bimal writes b, then we have to count the number of possible ways the Amal will win, number of possible draw and number of ways Bimal can win.
So, if the input is like a = 2; b = 4, then the output will be [2, 1, 3] so Amal can win in 2 possible ways. If the dice shows 3, there is a draw.
Steps
To solve this, we will follow these steps −
s1 := 0 s2 := 0 s3 := 0 if (a + b) mod 2 is same as 0, then: s2 := 1 if a is same as b, then: s2 := 6 otherwise when a > b, then: s1 := 6 - ((a + b) / 2) Otherwise s1 := (a + b - s2 - 1) / 2 s3 := 6 - s1 - s2 print s1, s2 and s3
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int a, int b) { int s1 = 0, s2 = 0, s3 = 0; if ((a + b) % 2 == 0) s2 = 1; if (a == b) s2 = 6; else if (a > b) s1 = 6 - ((a + b) / 2); else s1 = (a + b - s2 - 1) / 2; s3 = 6 - s1 - s2; cout << s1 << ", " << s2 << ", " << s3 << endl; } int main() { int a = 2; int b = 4; solve(a, b); }
Input
2, 4
Output
2, 1, 3
Advertisements