168.Excel Sheet Column Title
Problem description:
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
Difficulty:Easy
class Solution {
public:
string convertToTitle(int n) {
std::string titleStr = "";
int result = n;
int remain = 0;
char tmpc;
while(result != 0)
{
remain = result%26;
result = result/26;
if (remain == 0 && result != 0)
{
result--;
tmpc = 'Z';
}
else
{
tmpc = (remain +64) + '\0';
}
titleStr = tmpc + titleStr;
}
return titleStr;
}
};