一个 C++ 程序,用于根据电子表格中的列号找出电子表格列标题
假设我们有一个正整数,我们需要找出其在电子表格中显示的对应列标题。例如 [1 : A], [2 : B], [26 : Z], [27 : AA], [28 : AB] 等。
因此,如果输入是 29,则输出将是 AC。
要解决此问题,我们将遵循以下步骤:
当 n 不为零时,执行以下操作:
n := n − 1
res := res + n mod 26 + 'A' 的 ASCII
n := n / 26
反转数组 res
返回 res
让我们看看以下实现,以获得更好的理解:
例子
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string convertToTitle(int n) {
string res;
while(n){
res += (−−n)%26 + 'A';
n /= 26;
}
reverse(res.begin(), res.end());
return res;
}
};
main(){
Solution ob;
cout << (ob.convertToTitle(30));
}输入
30
输出
AD
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP