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实现代码:
/***************************************************************************** * @COPYRIGHT NOTICE * @Copyright (c) 2015, 楚兴 * @All rights reserved * @Version : 1.0 * @Author : 楚兴 * @Date : 2015/2/6 15:40 * @Status : Accepted * @Runtime : 1 ms *****************************************************************************/ #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution { public: string convertToTitle(int n) { string str = ""; while(n) { n--; str += n % 26 + 'A'; n /= 26; } reverse(str.begin(),str.end()); return str; } }; int main() { Solution s; for (int i = 25; i < 30; i++) { cout<<s.convertToTitle(i).c_str()<<endl; } system("pause"); }