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不翻译了,就是 171那道题反过来。
我这道题为什么写得这么丑,因为踏马的这道题允许每一位都在0~26范围内,而对于0的处理和26的处理还不一样。。。也可能是我脑子不清醒想不明白吧。哎,写代码也是要看状态的啊。。。
class Solution {
public:
string convertToTitle(int n) {
string ans;
while(n>26){
char temp;
if(n%26==0)
{
temp='Z';
n=(n-1)/26;
}
else
{
temp='A'+n%26-1;
n/=26;
}
ans.insert(0,&temp);
}
char temp;
if(n%26==0)
{
temp='Z';
n=(n-1)/26;
}
else
{
temp='A'+n%26-1;
n/=26;
}
ans.insert(0,&temp);
return ans;
}
};