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
The meaning of the topic is probably like this, given a positive number, return the column headings in the Excel table
In fact, from the pest can be seen in fact is a decimal to 26 binary problem
Here's my solution:
Programme one:
Using the Loop method, each time the remainder is converted into the corresponding character form, added to the string and finally the string is reversed
1 classSolution {2 Public:3 stringConverttotitle (intN) {4 if(n<1)return "";5 Else{6 stringresult="";7 Charch;8 while(n) {9n--;TenCH = n% -+'A'; Oneresult+=ch; AN/= -; - } - Reverse (Result.begin (), Result.end ()); the returnresult; - } - } -};
It is noteworthy that the n--is one of the problems that arises when N is a multiple of 26, and if it is the case that ch=n%26+64 will appear ' @ '
The second scenario:
In a recursive way
1#include <iostream>2#include <string>3 using namespacestd;4 classSolution {5 Public:6 stringConverttotitle (intN) {7 Static strings="";8 intp=0;9 if(n) {Tenn--; Onep = n% -; AConverttotitle (n/ -); - if(P) -s+= (Char) (p+'A'); the } - returns; - } - }; + intMain () { -Solution *s =Newsolution; + //cout<<s->converttotitle () <<endl; ACout<<s->converttotitle ( -); at return 0; -}
Because the static keyword is used, the function has a problem with successive calls, so there is no AC, just a way of thinking.
The first option is to be AC-
168 Excel Sheet Column Title