// Source : https://oj.leetcode.com/problems/excel-sheet-column-title/ // Author : Hao Chen // Date : 2014-12-25 /********************************************************************************** * * 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 * * Credits:Special thanks to @ifanchu for adding this problem and creating all test cases. * **********************************************************************************/ #include #include #include #include using namespace std; string base26_int2str(long long n) { string ret; while(n>0){ char ch = 'A' + (n-1)%26; ret.insert(ret.begin(), ch ); n -= (n-1)%26; n /= 26; } return ret; } long long base26_str2int(string& s){ long long ret=0; for (int i=0; i1){ n = atoll(argv[1]); } string ns = base26_int2str(n); n = base26_str2int(ns); cout << n << " = " << ns << endl; ns = "ABCDEFG"; if (argc>2){ ns = argv[2]; } cout << ns << " = " << base26_str2int(ns) << endl; }