A - ボウリングの記録 / Bowling Record Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
Convert the number of pins knocked down in each throw into the specified character. The problem simply asks to concatenate these converted characters in the input order and output the resulting string.
Analysis
The correspondence between the number of pins knocked down and the recorded character is predetermined:
-if \(0\) pins- The digit representing the number if \(1\) to \(9\) pins
Xif \(10\) pins
Using conditional branching (e.g., if/else) is fast enough to solve this problem. In this code, to make the conversion more concise, we use the following string as a lookup table:
const std::string table = "-123456789X";
Since string indexing in C++ is \(0\)-based, table[0] is -, table[1] is 1, …, and table[10] is X. That is, by directly using the number of pins knocked down as an index, we can retrieve the required character.
Note that outputting the input numbers directly would result in 0 for \(0\) and 10 for \(10\), which would be incorrect. Make sure to convert them to the specified characters.
For example, if the throw results are \(10, 0, 3, 9, 1\), the output should be X-391.
Algorithm
- Read the number of throws \(N\).
- Prepare the lookup table
table = "-123456789X"and an empty strings. - Read the number of pins \(A_i\) for each throw in order, and append
table[A_i]to the end ofs. - Output
s.
Since the correct character for each throw is appended one by one in the order of input, the resulting string s forms the desired record of length \(N\).
Complexity
- Time Complexity: \(O(N)\)
For each throw, we look up the corresponding character and append it to the end of the string. Appending to the string takes \(O(N)\) time in total. - Space Complexity: \(O(N)\)
We store the resulting string of length \(N\) for output. The lookup table has a fixed length.
Implementation Notes
By setting the order of characters in the lookup table to
-123456789X, the number of pins matches the index.Given the constraints \(0 \leq A_i \leq 10\),
table[A_i]is guaranteed to be within the bounds of the lookup table.Output all the characters concatenated into a single line without any spaces.
Source Code
// 問題文から独立に実装: 記号表を引く
#include <iostream>
#include <string>
int main() {
int n; std::cin >> n;
const std::string table = "-123456789X";
std::string s;
for (int i = 0; i < n; ++i) { int a; std::cin >> a; s += table[a]; }
std::cout << s << std::endl;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: