C++ code to count number of dice rolls to get target x


Suppose we have a number x. We have a six-faced dice and its faces are numbered from 2 to 7. We want exactly x points from the dice. When we throw the dice the face number will be added up to reach our target. We do not really care about the number of dice rolls, so we just want to know any number of rolls we can make to be able to get exactly x points for them. We are very lucky, so if the probability to get x points with chosen number of rolls is non-zero, we will be able to roll the dice in such a way. We have to find the number.

So, if the input is like x = 100, then the output will be 27, because we get 2, 11 times, 3, six times and 6, 10 times. (Other answers are also possible)

Steps

To solve this, we will follow these steps −

return floor of (x / 2)

Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h>
using namespace std;
int solve(int x){
   return x/2;
}
int main(){
   int x = 100;
   cout << solve(x) << endl;
}

Input

100

Output

50

Updated on: 15-Mar-2022

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements