假设我们有一个电子表格的列标题。我们知道电子表格的列号是字母的。它从A开始,在Z之后,将是AA,AB,ZZ,然后是AAA,AAB,ZZZ,依此类推。所以第1列是A,第27列是Z。在这里,我们将看到如果给出列数,则如何获取列字母。因此,如果列号为80,则它将为CB。因此,我们必须从编号中找到相应的列标题。如果输入为30,则将为AD。
#include<iostream>
#include<algorithm>
using namespace std;
void showColumnLetters(int n) {
string str = "";
while (n) {
int rem = n%26;
if (rem==0) {
str += 'Z';
n = (n/26)−1;
}
else{
str += (rem-1) + 'A';
n = n/26;
}
}
reverse(str.begin(), str.begin() + str.length());
cout << str << endl;
}
int main() {
int n = 700;
cout << "Cell name of " << n << " is: ";
showColumnLetters(700);
}700
输出结果
单元格名称700是:ZX