Bowling
InterviewTime limit1sMemory limit128 MB
Given the pins knocked down on each delivery of a valid ten-frame bowling game, compute the total score.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Array, Math
- Solved
- No attempts yet
Problem
Bowling is a sport in which a player rolls a ball down a lane to knock down as many of the ten pins as possible. Scoring can be confusing for beginners, especially because the points for a strike or a spare are not added immediately.
A game has ten frames. Each frame starts with a full set of ten pins, and normally you roll the ball twice per frame, trying to knock down all ten pins.
- Strike: you knock down all ten pins with the first ball of the frame. Its score is 10 plus the pins from your next two balls. For example, a strike in the first frame, then 7 and 1 in the second frame, scores for the first frame and for the second, for 26 after two frames.
- Spare: you knock down all ten pins using both balls of the frame. Its score is 10 plus the pins from your next ball. For example, a spare of 6 and 4 in the first frame, then 8 and 1 in the second frame, scores for the first frame and for the second, for 27 after two frames.
- Open frame (neither a strike nor a spare): its score is simply the number of pins knocked down in the frame.
The tenth (final) frame is slightly different so that strike and spare bonuses can be counted.
- If you strike on the first ball, you get two more balls, for three deliveries in total. Three strikes in the final frame score .
- If you make a spare, you get one more ball. A spare of 9 and 1 followed by a strike scores .
Given the pins knocked down on every delivery, compute the player's total score for the game.
Input
The first line contains an integer (), the number of test cases. Each of the next lines describes one full game as a sequence of integers (), the number of pins knocked down on each delivery, in order. Every game in the input is valid.
Output
For each test case, print the player's total score for that game on its own line.