Bowling Score Calculation
Time limit1sMemory limit256 MB
Given a string describing each ball's result in a 10-frame bowling game (S for strike, P for spare, - for zero, digits otherwise), compute the final score using strike and spare bonus rules.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, String, Array
- Solved
- No attempts yet
Problem
Sohyun went to a bowling alley with her friends. Because of an error in the alley's system, the scoreboard failed to tally the scores. It was too cold outside, so Sohyun and her friends just decided to play anyway. The game has ended, but they cannot work out how many points each of them scored. Write a program that calculates bowling scores for Sohyun and her friends.
Bowling rules
- A game consists of 10 frames.
- In each frame, 10 pins are set up and knocked down with a ball, and each pin is worth 1 point by default.
- Each frame gives two chances, and knocking down all 10 pins on the first chance is called a strike (S).
- Knocking down all 10 pins using both chances is called a spare (P).
- A strike earns bonus points equal to the number of pins knocked down over the next two chances.
- A strike in any frame other than the 10th removes the second chance of that frame.
- A spare earns bonus points equal to the number of pins knocked down on the next chance.
- A strike in the final 10th frame gives two bonus chances. No bonus points are awarded during these two bonus chances.
- A spare in the final 10th frame gives one bonus chance. No bonus points are awarded during this bonus chance.
Input
The first line gives the number of pins Sohyun knocked down on each chance, with no spaces. A strike is given as S, a spare as P, and knocking down no pins as -.
Output
Print Sohyun's score on the first line.
Hint
Recording a strike on every chance of a game is called a perfect game. The score is then 300, which is possible because in the frame after consecutive strikes, knocking down pins is worth 3 times rather than 2 times the base value. (base score + bonus from the strike two frames back + bonus from the strike one frame back)