Excel表列名称

给定一个正整数,返回它在 Excel 表中相对应的列名称。

例如,

    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB 
    ...

示例 1:

输入: 1输出: "A"

示例 2:

输入: 28输出: "AB"

示例 3:

输入: 701输出: "ZY"


public string ConvertToTitle(int n) {
    StringBuilder res = new StringBuilder();
    int temp = 0;
    while(n > 0)
    {
        temp = n % 26;
        n = n / 26;
        if(temp == 0)
        {
            //特殊的26的倍数处理
            temp = 26;
            n = n - 1;
        }
        res.Insert(0,((char)(temp+64)));
    }

    return res.ToString();

}

首页 我的博客
粤ICP备17103704号