码迷,mamicode.com
首页 > 其他好文 > 详细

求excel某一列对应的字母表示

时间:2014-11-02 22:39:25      阅读:194      评论:0      收藏:0      [点我收藏+]

标签: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;
}

bubuko.com,布布扣


第二种处理方法:

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

所以,数组下标是从0开始的,没有0真是烦啊。。。。



求excel某一列对应的字母表示

标签:blog   for   sp   div   on   2014   log   amp   ad   

原文地址:http://blog.csdn.net/taoqick/article/details/40712563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!