Given an integer columnNumber
, return its corresponding column title as it appears in an Excel sheet.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
Example 1:
Input: columnNumber = 1 Output: "A "
Example 2:
Input: columnNumber = 28 Output: "AB "
Example 3:
Input: columnNumber = 701 Output: "ZY "
Constraints:
1 <= columnNumber <= 231 - 1
program main
implicit none
integer, parameter :: columnNumber = 1
character(len=5) :: columnTitle
call getColumnTitle(columnNumber, columnTitle)
print '(A)', trim(columnTitle)
contains
subroutine getColumnTitle(columnNumber, columnTitle)
implicit none
integer, intent(in) :: columnNumber
character(len=*), intent(out) :: columnTitle
character(len=1), dimension(:), allocatable :: letters
integer :: i, n
allocate(letters(columnNumber))
do i = 1, size(letters)
letters(i) = char(iachar('A') + mod(columnNumber - 1, 26))
columnNumber = (columnNumber - 1) / 26
end do
n = size(letters)
if (n > 0) then
columnTitle = repeat(' ', n-1) // letters(n)
else
columnTitle = ''
end if
end subroutine getColumnTitle
end program main
temp.f95:24:12: 24 | columnNumber = (columnNumber - 1) / 26 | 1 Error: Dummy argument ‘columnnumber’ with INTENT(IN) in variable definition context (assignment) at (1)
```
PROGRAM columnTitle
IMPLICIT NONE
INTEGER :: columnNumber
CHARACTER(LEN=3) :: columnTitle
! Read input
READ(*,*) columnNumber
! Calculate column title
columnTitle = getColumnTitle(columnNumber)
! Print output
WRITE(*,*) columnTitle
CONTAINS
FUNCTION getColumnTitle(columnNumber) RESULT(columnTitle)
IMPLICIT NONE
INTEGER, INTENT(IN) :: columnNumber
CHARACTER(LEN=3) :: columnTitle
INTEGER :: remainder
! Initialize column title
columnTitle = ""
! Loop through digits of column number
DO WHILE (columnNumber > 0)
! Calculate remainder
remainder = MOD(columnNumber, 26)
! Add digit to column title
IF (remainder == 0) THEN
columnTitle = "Z" // columnTitle
columnNumber = columnNumber / 26 - 1
ELSE
columnTitle = CHAR(remainder + 64) // columnTitle
columnNumber = columnNumber / 26
END IF
END DO
END FUNCTION getColumnTitle
END PROGRAM columnTitle
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:5:35: 5 | CHARACTER(LEN=3) :: columnTitle | 1 Error: Symbol ‘columntitle’ at (1) cannot have a type temp.f95:11:16: 11 | columnTitle = getColumnTitle(columnNumber) | 1 Error: ‘columntitle’ at (1) is not a variable temp.f95:14:26: 14 | WRITE(*,*) columnTitle | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:43:2: 43 | ``` | 1 Error: Invalid character in name at (1) temp.f95:35:16: 35 | columnNumber = columnNumber / 26 - 1 | 1 Error: Dummy argument ‘columnnumber’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:38:16: 38 | columnNumber = columnNumber / 26 | 1 Error: Dummy argument ‘columnnumber’ with INTENT(IN) in variable definition context (assignment) at (1)
def convert_to_title(columnNumber: int) -> str:
result = ""
while columnNumber:
columnNumber -= 1
result = chr(columnNumber % 26 + ord('A')) + result
columnNumber //= 26
return result
The algorithm converts an integer `columnNumber` to its corresponding column title as it appears in an Excel sheet. It does so by treating the Excel sheet column headers as a Base-26 number system where the numbers are represented by the characters from A-Z.
result
that will store the final column title.columnNumber
is greater than 0.columnNumber
, as the base representation starts from A (1).columnNumber
divided by 26 is the index of the character in the A-Z range, which is added to the result
string.columnNumber
is then divided by 26 and the loop continues until columnNumber
is 0.result
string is returned as the corresponding column title.#include <string>
std::string convertToTitle(int columnNumber) {
std::string result = "";
while (columnNumber) {
columnNumber--;
result = (char)(columnNumber % 26 + 'A') + result;
columnNumber /= 26;
}
return result;
}
The algorithm converts an integer `columnNumber` to its corresponding column title as it appears in an Excel sheet. It does so by treating the Excel sheet column headers as a Base-26 number system where the numbers are represented by the characters from A-Z.
result
that will store the final column title.columnNumber
is greater than 0.columnNumber
, as the base representation starts from A (1).columnNumber
divided by 26 is the index of the character in the A-Z range, which is added to the result
string.columnNumber
is then divided by 26 and the loop continues until columnNumber
is 0.result
string is returned as the corresponding column title.