Excel Sheet Column Title

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 

Code:

 string convertToTitle(int n) {
        //这题竟然没有做出来,(n-1)%26(这个式子老是写成n%26-1)与(n-1)/26(这个式子老是搞不清粗是n/26还是n/27)这两个式子一直不清楚
        string res = "";  
        while(n)  
        {  
            res = (char)('A' + (n-1)%26) + res;  
            n = (n-1) / 26;  
        }  
        return res;
    }

 

posted @ 2015-06-22 10:23  Rosanne  阅读(142)  评论(0编辑  收藏  举报