C ++中的Excel工作表列号
我们知道excel列号是字母的。它从A开始,在Z之后,将是AA,AB,ZZ,然后是AAA,AAB,ZZZ,依此类推。因此,第1列是A,第27列是Z。在这里,我们将看到如果给出多个列,则如何获取列字母。因此,如果列号为80,则它将为CB。
假设我们有一个数字n,其值是28,那么我们需要提醒输入26。如果余数是0,则数字是26、52,依此类推。然后我们将Z放入输出字符串中。n的值变为n/26–1。如果余数不为零,则只需将相应的字符插入字符串中,然后执行n=n/26。最后,将打印字符串的反面。
范例(C++)
#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);
}输入值
Cell number: 700
输出结果
Enter cell number:700 Cell name of 700 is: ZX
热门推荐
10 祝福语简短大全微信
11 祝父母节日祝福语简短
12 简短中秋祝福语朋友结婚
13 幽默的离职祝福语简短
14 群众过春节祝福语简短
15 14年祝福语简短情话
16 英语寄语生日祝福语简短
17 公司新年祝福语简短的
18 要过年了祝福语简短