- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to count how many ways two players win or make 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
- Related Articles
- Minimum Players required to win the game in C++
- Program to check whether first player can win a game where players can form string char by char in C++
- C++ program to count in how many ways we can paint blocks with two conditions
- Program to count how many swimmers will win the final match in Python
- Program to check whether Amal can win stone game or not in Python
- Program to count how many ways we can divide the tree into two trees in Python
- Program to count number of ways to win at most k consecutive games in Python
- Program to check whether first player win in candy remove game or not in Python?
- C++ program to count in how many ways we can minimize cable length to connect computers
- Program to find out if we win in a game in Python
- How many players make 80 runs and above from the following histogram?"\n
- Program to check person 1 can win the candy game by taking maximum score or not in Python
- Maximum number of dots after throwing a dice N times in C++
- Program to find number of moves to win deleting repeated integer game in Python
- Find the number of players who roll the dice when the dice output sequence is given in C++

Advertisements