#C12975. Excel Column Title and Number Conversion
Excel Column Title and Number Conversion
Excel Column Title and Number Conversion
This problem involves converting between an integer and its corresponding Excel column title and vice versa.
In Microsoft Excel, columns are labeled as follows: 1 represented as A, 2 as B, ... 26 as Z, 27 as AA, 28 as AB, and so on. The conversion formulas are given by:
For number_to_title function: \[ \text{while } n > 0:\ \quad n \mathrel{-}= 1\ \quad \text{append } \operatorname{chr}(n \mod 26 + \operatorname{ord}('A'))\ \quad n = \lfloor n/26 \rfloor \]
For title_to_number function: \[ \text{for each character } c\text{ in } s:\ \quad \text{result} = \text{result} \times 26 + (\operatorname{ord}(c) - \operatorname{ord}('A') + 1) \]
Your task is to implement both conversions in a single program that reads input from stdin and prints the result to stdout.
The program first reads an integer Q representing the number of queries. Each of the next Q lines contains a query. Each query begins with a character that indicates the type of conversion:
- 'N': followed by an integer. Convert the integer to its corresponding Excel column title.
- 'T': followed by a string. Convert the Excel column title to its corresponding integer.
Output each result on a new line.
inputFormat
The first line contains a positive integer Q, the number of queries.
Each of the next Q lines contains a query in one of the following formats:
N x
: wherex
is a positive integer. You need to convertx
to the corresponding Excel column title.T s
: wheres
is a string representing an Excel column title. You need to converts
to its corresponding integer.
Input is read from stdin
.
outputFormat
For each query, output the result in a separate line to stdout
. If the query type is 'N', output the Excel column title. If the query type is 'T', output the corresponding integer.
2
N 28
T AB
AB
28