- 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

# Find the number of players who roll the dice when the dice output sequence is given in C++

Suppose we have a string S and a number X. There are M different players who roll the dice. one player keeps on rolling the dice until he gets a number other than X. Here in the string S, S[i] represents the number at ith roll of a dice. We have to find the value of M. One constraint is that the last character in S will never be X. So for example, if string is “3662123” and X = 6, the output will be 5. This can be described as follows −

- First player rolls and got 3
- Second player rolls, and got 6, 6 and 2
- Third player rolls, and got 1
- Fourth player rolls, and got 2
- Fifth player rolls, and got 3

The task is simple, we will traverse the string, and count the number of characters, that are not X, the count will be the answer.

## Example

#include<iostream> using namespace std; int countPlayers(string str, int x) { int count = 0; for (int i = 0; i < str.size(); i++) { if (str[i] - '0' != x) count++; } return count; } int main() { string s = "3662123"; int x = 6; cout << "Number of players: " << countPlayers(s, x); }

## Output

Number of players: 5

- Related Articles
- Dice Roll Simulation in C++
- When a dice is thrown once, find the probability of getting an even number.
- A dice is thrown. Find the probability of getting an even number.
- A dice is a three-dimensional shape with six faces and all of them are identical squares. The length of an edge of the dice is given by ( x ). Find the total length of the edges of the dice.
- Two unbiased dice are thrown. Find the probability that the total of the numbers on the dice is greater than 10.
- In a simultaneous throw of a pair of dice, find the probability of getting a number other than 5 on any dice.
- Two dice arc rolled once. Find the probability of getting such numbers on the two dice, whose product is 12.
- Two different dice are tossed together. Find the probability that the product of the two numbers on the top of the dice is 6.
- A dice is thrown once. Find the probability of getting a number less than 5.
- C++ program to count how many ways two players win or make draw in dice throwing game
- Two dice are thrown simultaneously. What is the probability that 5 will come up at both dice?
- Find the probability of getting the multiple of 6 on throwing a dice
- Number of Dice Rolls With Target Sum in Python
- Two different dice are tossed together. Find the probability :$( i)$ of getting a doublet $( ii)$ of getting a sum 10, of the numbers on the two dice.
- Maximum number of dots after throwing a dice N times in C++

Advertisements