168 Excel Sheet Column Title
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> ABmap<int, char> mapAlpha() {
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;
map<int, char> alphaInteger;
for (auto c : alphabet) {
alphaInteger[count % 26] = c;
count++;
}
return alphaInteger;
}
int findMaxPower(int n) {
int max = 0;
int count = 0;
while (n > max) {
max = pow(26, count++);
}
count = count - 2;
return count > 0 ? count : 0;
}
string convertToTitle(int n, map<int, char> &alphaMap) {
string s = "";
if (n == 0)
return s = "invalid";
int max = findMaxPower(n);
while (max >= 0) {
int key = int(floor(n / pow(26, max--))) % 26;
s += alphaMap.find(key)->second;
}
return s;
}
int main()
{
map<int, char> alphaMap = mapAlpha();
cout << convertToTitle(52, alphaMap) << endl;
for (int i = 0; i < 1000; i++) {
cout << convertToTitle(i, alphaMap) << " ";
}
}Last updated