标签:blog for sp div on 2014 log amp ad
其实就是把一个十进制数转换成26进制,但是这个26进制数没有0,只有1-26:
两种处理方法:
#include <assert.h> #include <algorithm> #include <vector> using namespace std; const int radix = 26; string getCol(int num) { string res; while (num != 0) { res.push_back((num-1)%radix+'A'); num = (num-1)/radix; } reverse(res.begin(), res.end()); return res; } int main() { string res = getCol(702); return 0; }
#include <assert.h> #include <algorithm> #include <vector> using namespace std; const int radix = 26; string getCol(int num) { string res; int i = 1, j = 0, t = i; for (i = 1; i * radix + radix < num; i = radix * i); while (i != 0) { // 26*26+26 和 直接26 int remainder = num % i; int divide = (remainder == 0 && i != 1) ? (num / i - 1) : (num / i); // 因为26进制数没有0, 所以余数要借radix个,并使得商-1, 但是如果divisor = 1, 就不用这么做了 res.push_back(divide + 'A' - 1); num = remainder == 0 ? radix : remainder; i = i / radix; } return res; } int main() { string res = getCol(884); return 0; }
标签:blog for sp div on 2014 log amp ad
原文地址:http://blog.csdn.net/taoqick/article/details/40712563