QUESTION
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1, A 2, B 3, C ... AA, Z
First TRY
classSolution { Public: stringConverttotitle (intN) {intremain = n% -; N/= -; stringRET =""; Charch; while(1) { if(n = =0&& remain = =0) { returnret; } Else if(n = =0) {ch=GetChar (remain); returnCH +ret; } CH=GetChar (remain); RET= ch + ret;//char-string, direct concatremain = n% -; N/= -; } returnret; } CharGetChar (intN) {if(n! =0) return 'A'+ N-1; Else return 'Z'; }};
Result:wrong
Input:26
Output: "AZ"
Expected: "Z"
SECOND TRY
Note that the remainder is 0.
classSolution { Public: stringConverttotitle (intN) {intremain = n% -; N/= -; stringRET =""; Charch; while(1) { if(n = =0&& remain = =0) { returnret; } Else if(n = =0) {ch=GetChar (N,remain); returnCH +ret; } CH=GetChar (N,remain); RET= ch + ret;//char-string, direct concatremain = n% -; N/= -; } returnret; } CharGetChar (int& N,intremain) { if(Remain! =0) return 'A'+ Remain-1; Else{n-=1; return 'Z'; } }};
Excel Sheet Column Title (String-type convertion)