
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 Number of Dice Rolls to Get Target X in C++
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