您的位置:首页 > 其它

LeetCodeOJ_168_Excel Sheet Column Title

2015-07-20 12:26 381 查看
答题链接

题目:

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

代码:

“`

class Solution {

public:

string convertToTitle(int n) {

string str;

string c;

int m=n;

while(n>0)

{

m=n%26;

if(m==0)

m=26;

n=(n-m)/26;

switch(m)

{

case 1:{ c=”A”; break;}

case 2:{ c=”B”; break;}

case 3:{ c=”C”; break;}

case 4:{ c=”D”; break;}

case 5:{ c=”E”; break;}

case 6:{ c=”F”; break;}

case 7:{ c=”G”; break;}

case 8:{ c=”H”; break;}

case 9:{ c=”I”; break;}

case 10:{ c=”J”; break;}

case 11:{ c=”K”; break;}

case 12:{ c=”L”; break;}

case 13:{ c=”M”; break;}

case 14:{ c=”N”; break;}

case 15:{ c=”O”; break;}

case 16:{ c=”P”; break;}

case 17:{ c=”Q”; break;}

case 18:{ c=”R”; break;}

case 19:{ c=”S”; break;}

case 20:{ c=”T”; break;}

case 21:{ c=”U”; break;}

case 22:{ c=”V”; break;}

case 23:{ c=”W”; break;}

case 24:{ c=”X”; break;}

case 25:{ c=”Y”; break;}

case 26:{ c=”Z”; break;}

}

str.insert(0,c.c_str());

}

return str;

}

};

结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: