Write a program that simulates a coin-tossing game. At the beginning of the game, each of N players has a single fair coin and M points. The game performs a sequence of passes until only a minimum number of players remain. In a pass the players toss their coins into the air and when the coins fall to earth, the players count the number of coins that show heads and the number of coins that show tails. If the number of heads exceed the number of tails, then the players who tossed heads increase their points by an integer P and the players who tossed tails decrease their points by the same integer P. The players perform the opposite actions when the number of tails is greater than the number of heads. If the number of heads equals the number of tails, then no player’s points change. If any player’s points fall below one, then the player leaves the game, never to return. The game employs a second integer K to compute P’s value. K’s and P’s initial values are one. After each pass, the game adds K to P and then adds one to K. After the first pass P’s value is two and K’s value is two. After the second pass, P’s value is four and K’s value is three. After the third pass, P’s value is seven and K’s value is four. After the fourth pass, P’s value is eleven and K’s value is five. In general, P(k) = 1 + k(k - 1) / 2 What is the minimum number of players? It is your responsibility to determine the minimum number of players.
Please code in C++.
Write a program that simulates a coin-tossing game. At the beginning of the game, each of N players has a single fair coin and M points. The game performs a sequence of passes until only a minimum number of players remain. In a pass the players toss their coins into the air and when the coins fall to earth, the players count the number of coins that show heads and the number of coins that show tails. If the number of heads exceed the number of tails, then the players who tossed heads increase their points by an integer P and the players who tossed tails decrease their points by the same integer P. The players perform the opposite actions when the number of tails is greater than the number of heads. If the number of heads equals the number of tails, then no player’s points change. If any player’s points fall below one, then the player leaves the game, never to return.
The game employs a second integer K to compute P’s value. K’s and P’s initial values are one. After each pass, the game adds K to P and then adds one to K. After the first pass P’s value is two and K’s value is two. After the second pass, P’s value is four and K’s value is three. After the third pass, P’s value is seven and K’s value is four. After the fourth pass, P’s value is eleven and K’s value is five. In general,
P(k) = 1 + k(k - 1) / 2
What is the minimum number of players? It is your responsibility to determine the minimum number of players.
Before the game begins the program should display the players’ names, their points, and P and K. After each pass, the program should display the number of heads tossed, the number of tails tossed, the number of players tossing coins, and P and K. In addition to this information, for each player tossing a coin, the program should display the player’s name, coin toss outcome – heads or tails, and the player’s points. If a player’s points are less than zero, the program should display zero. Do not display information about players who have left the game. After the game ends the program should display the names of the players with the maximum number of points, and P and K. To be clear, there is only one maximum number of points.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images