面试网易游戏遇到的一道题:
excel的列数是A,B,C,...,Z,AA,AB,AC.......AZ,BA,BB...BZ.
要求写一个函数,给你一个数字你就能得出对应的列数,例如27对应AA,28对应AB
下面是C++代码,
#include <iostream>
#include <string>
using namespace std;
string fun(int i){
if(i<=0)
return "error";
string s;
while(i>26){
int shang=i/26;//
int yushu=i%26;
if(yushu==0){
yushu=26;
shang--;
s.insert(0,char(yushu+64));//在0处插入char(yushu+64)
}
else{
s.insert(0,char(yushu+64));
}
i=shang;
}
s.insert(0,char(i+64));
return s;
}