首页 > 其他 > 详细

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

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

其实就是把一个十进制数转换成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某一列对应的字母表示

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!