LeetCodeOJ_168_Excel Sheet Column Title

答題鏈接

題目:

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;
}
};

結果:

發佈了32 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章