It took a long time to finally solve the problem, recording the idea of solving the problem:
Select 26 to solve this problem, the 26 characters {0,a,b,...,y} represent the Arabic numerals {0,1,2,...,25} respectively.
If according to our practice, 26 should be a0,52 for B0 and so on.
Don't be impatient, when we get such a result, we start to convert, taking A0 as an example:
A0 = 1x26^1 +0x26^0
= 0x26^1 +26x26^0
=z;
Similarly for B0:
B0 = 2x26^1 +0x26^0
= 1x26^1 +26x26^0
=az;
That is, the high 1 represents the low 26, the high position minus 1, the low plus 26, in this way we realized the conversion.
Here's the code:
Class Solution { Public:string Converttotitle(intN) {stringarr[ -] = {"0","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y"};stringResintQuotient,remainder; quotient = n; for(; Quotient! =0; ) {remainder = quotient% -; quotient = quotient/ -;if(Remainder = =0) {res ="Z"+ res; quotient = quotient-1; }Elseres = Arr[remainder] + res; }returnRes }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Excel Sheet Column Title